我试图将某些内容发布到服务器并将数据返回给我的应用程序。这是我的服务器代码。
header("Access-Control-Allow-Origin:*");
// database
require("db_config.php");
//$_POST['city'] = 42;
$query = "SELECT * FROM locations WHERE city = :city GROUP BY category";
$query_params = array(':city' => $_POST['city']);
try {
$stmtOdgovor = $conn->prepare($query);
$resultOdgovor = $stmtOdgovor->execute($query_params);
} catch(PDOException $ex){
$response["success"] = 0;
$response["message"] = "Database error";
die(json_encode($response));
}
$rowsOdgovor = $stmtOdgovor->fetchAll();
if ($rowsOdgovor){
$responseOdgovor = array();
foreach ($rowsOdgovor as $rowOdgovor) {
$postOdgovor["category"] = $rowOdgovor["category"];
array_push($responseOdgovor, $postOdgovor);
}
echo json_encode($responseOdgovor);
} else {
$response["success"] = 0;
$response["message"] = "No locations available";
}
这是我的客户代码:
$http({
method: "POST",
url: 'http://glutenfree.hr/rest/all_cities.php',
dataType:'json',
data:{'city':$stateParams.cityId},
headers : {'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8' }
}).then(function successCallback(response) {
alert();
//response.data ti je to
console.log(response);
}, function errorCallback(response) {
console.log(response);
});
当我在php中编写thos参数时,它可以正常工作,但是当我用角度发布它时,它确实是这样的。谁能解释一下是什么问题?谢谢。