I have a angular app using jquery for the ajax (I made a little more progress with this issue using jquery for it over $http). I am trying to send json data to php and doing it successfully (php reads it and returns the data through the header). My problem here is $.ajax is failing to recognize a success, it fails and returns parse error for the response.
Here is my javascript:
function loginUser() {
var target = "../data/user.php";
var data = {
"userName": "me",
"userPass": "pass",
"action": "login"
};
$.ajax({
url: target,
type: "POST",
data: JSON.stringify(data),
contentType: 'application/json; charset=utf-8',
dataType: "json",
success: function(data) {
console.log(data);
}
}).fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
}
Here is my php:
$data = file_get_contents("php://input");
$objData = json_decode($data);
print_r($objData);
Here is the response I get in the header from php:
stdClass Object
(
[userName] => me
[userPass] => pass
[action] => login
)
Thanks a lot for the help
$.ajaxuse Angular's$httpdocs.angularjs.org/api/ng/service/$httpphp://inputeither. If you set the header toContent-Type: application/x-www.form-urlencodedyou can just use the regular php variables ($_GET,$_POST,$_REQUEST) on the other end. If you're sending a JS object just use jQuery's param function when sending ->$.param(data)