Passing JSON data through jQuery ajax.post for PHP

Passing data to php file via AJAX using POST. It did a great job of sending strings, but now I wanted to represent my JS object using JSON and decode it on the PHP side.

In the console, I see that my data is sent correctly, but on the PHP side, json_decode returns NULL.

I tried the following:

this.getAbsence = function() { alert(JSON.stringify(this)); jQuery.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "ajax/selectSingle.php?m=getAbsence", data: JSON.stringify(this), success : function(data){ alert(data); } }); } 

PHP:

 echo $_POST['data']; echo json_decode($_POST['data']); echo var_dump(json_decode($_POST['data'])); 

and

 this.getAbsence = function() { alert(JSON.stringify(this)); jQuery.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "ajax/selectSingle.php?m=getAbsence", data: {'Absence' : JSON.stringify(this)}, success : function(data){ alert(data); } }); } 

PHP:

 echo $_POST['Absence']; echo json_decode($_POST['Absence']); echo var_dump(json_decode($_POST['Absence'])); 

The warning was just to check, everything is in order ...

And the regular line was correctly reflected :-)

+6
source share
4 answers

If you were mistaken in your code in the first code, you should use this:

 var_dump(json_decode(file_get_contents("php://input"))); //and not $_POST['data'] 

Quote from the PHP manual

php: // input is a read-only stream that allows you to read raw data from the request body.

Since in your case you are sending JSON to the body, you must read it from this stream. The usual $_POST['field_name'] method does not work because the message body is not in URLencoded format.

In the second part, you should have used this:

 contentType: "application/json; charset=utf-8", url: "ajax/selectSingle.php?m=getAbsence", data: JSON.stringify({'Absence' : JSON.stringify(this)}), 

UPDATE

When the request is of the application/json content type, PHP will not parse the request and provide you with a JSON object in $_POST , you must parse it yourself from the raw HTTP body. JSON string is retrieved using file_get_contents("php://input"); .

If you must get this using $_POST , you must do this:

 data: {"data":JSON.stringify({'Absence' : JSON.stringify(this)})}, 

And then in PHP do:

 $json = json_decode($_POST['data']); 
+23
source

Single quotes are not valid for php json_encode , use double quotes for both field names and values.

0
source

It seems to me that you should reformat your AJAX object. The URL property should only be the URL of the target php file, and any data that needs to be published should be in the form of a query string in the data property. The following should work as you expected:

 this.getAbsence = function() { var strJSONData = JSON.stringify(this); alert(strJSONData); jQuery.ajax({ type: 'POST', contentType: 'application/json; charset=utf-8', url: 'ajax/selectSingle.php', data: 'm=getAbsence&Absence=' + strJSONData, success: function(data) { alert(data); } }); } 
0
source

try it

  var vThis = this; this.getAbsence = function() { alert(JSON.stringify(vThis)); jQuery.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "ajax/selectSingle.php?m=getAbsence", data: JSON.stringify(vThis), success : function(data){ alert(data); } }); } 

EDIT

I think we can do it too!

  var vThis = this; this.getAbsence = function() { alert(JSON.stringify(vThis)); jQuery.ajax({ type: "POST", dataType: "json", url: "ajax/selectSingle.php?m=getAbsence", data: vThis, success : function(data){ alert(data); } }); } 

and in php

 print_r($_POST); 
0
source

Source: https://habr.com/ru/post/917618/


All Articles