Why can't I get my JSON Data posted using AJAX in my PHP file? Why can't I get my JSON Data posted using AJAX in my PHP file? ajax ajax

Why can't I get my JSON Data posted using AJAX in my PHP file?


The problem is that dataType: "json" doesn't mean that you're posting json, but that you're expecting to receive json data from the server as a result of your request. You could change your post data to:

data: {myPostData : "{'lastName':'" + _lname + "','firstName':'" + _fname + "','middleName':'" + _mname + "'}"}

and then parse it on your server like

$myPostData = json_decode($_POST['myPostData']);$firstname = $myPostData["firstName"];$lastname = $myPostData["lastName"];$middlename = $myPostData["middleName"];


One issue- you're using single quotes for your json. You should be using double quotes (according to spec).

{"lastName":"Smith", "firstName":"Joe"}instead of {'lastName':'Smith', 'firstName':'Joe'}