javascript - Unable to read POST'd data using $.ajax -


i have super simple js code posts local server. both js , php on localhost i'm not worrying cross site issues @ moment. here's js:

var endpoint_base = ''; var endpoint_user_login    = { url: '/user/login',    method: 'post'}; var endpoint_user_register = { url: '/user/register', method: 'post'};  var tosend = {'credentials': { 'email':'1', 'password':'123'}};  $.ajax({     method: endpoint_user_login.method,     url: endpoint_base + endpoint_user_login.url,     contenttype: "application/json",     data: tosend }); 

my php simply:

<?php print_r($_post); 

in chrome debugger can see network call made, response received in $_post empty, ie prints out array().

if add echo "abc"; in php can see in response, hitting url correctly.

i'm using jquery 2.1.4.

the js console isn't printing out error messages.

any ideas wrong? thanks!

php not populating $_post expect because setting header json. believe default url-form-encoded.

either don't define content type or if wish send actual json achieve doing like:

$postdata    = file_get_contents('php://input'); $request     = json_decode($postdata); $credentials = $request->credentials; 

Comments