[英]How to post json data to a php script with $http.post
我一直有這個問題一段時間,並嘗試了許多無效的解決方案。 我正在嘗試使用$ http.post將json數據發送到名為processCustomers.php的php腳本,這是我的API的一部分
該職位要求是
angular.extend(obj, {id: id});
$http({
method: 'POST',
url: '../API/processCustomers.php',
headers: { 'Content-Type': 'application/json' },
dataType: 'json',
data: obj
}).success(function (data) {
console.log(data);
});
};
並且processCustomers是
$newCustomers = filter_has_var(INPUT_POST, 'newCustomer');
if ($newCustomers) {#request to add new customer
$exceptionalFields = ['customerID']; //
$customersJSON = json_decode(filter_input(INPUT_POST, "newCustomer"));
if (json_last_error() > 0) {
echo DotCom::systemFeedBack("Sorry customer not created unknown error", "data sent is not a json object", 303);
return;
}
foreach ($customersJSON as $key => $customer) {
$parameterTypes = ""; # order determined by the sql returned by validateInputData()
$entries = $func->validateInputData($tableMetaData,$customer, $parameterTypes, $exceptionalFields);
if (!is_array($entries)) {
echo $entries;
return;
}
$results = $customers->createCustomer($entries, $parameterTypes, $link,$status);
當我發送請求時,瀏覽器中的控制台顯示響應為text / html而不是application / json。我瀏覽了許多教程和示例,還嘗試了以下操作:刪除標頭和數據類型-包裝obj無效在JSON.stringify中-導致標題更改為application / x-www-form-urlencoded
您的php文件中需要以下內容:
$json = file_get_contents('php://input');
$obj = json_decode($json);
在標頭中傳遞Content-Type: application/json
時, $_POST
Content-Type: application/json
$_POST
將為空。
從這個問題中學到了幾天
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.