javascript - How to post json data to a php script with $http.post -
i've been having problem while , tried many solutions haven't worked. trying use $http.post send json data php script named processcustomers.php part of api
the post request is
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); }); };
and processcustomers is
$newcustomers = filter_has_var(input_post, 'newcustomer'); if ($newcustomers) {#request 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 not json object", 303); return; } foreach ($customersjson $key => $customer) { $parametertypes = ""; # order determined sql returned validateinputdata() $entries = $func->validateinputdata($tablemetadata,$customer, $parametertypes, $exceptionalfields); if (!is_array($entries)) { echo $entries; return; } $results = $customers->createcustomer($entries, $parametertypes, $link,$status);
when send request, console in browser says response text/html instead of application/json.i looked through many tutorials , examples , have tried folowings: removing headers , datatype - no effect wrapping obj in json.stringify - results in header changing application/x-www-form-urlencoded
you need following in php file:
$json = file_get_contents('php://input'); $obj = json_decode($json);
$_post
empty when content-type: application/json
passed in headers.
learned few days this questions
Comments
Post a Comment