SamiSalami
SamiSalami

Reputation: 667

Submitting JSON data via JQuery ajax.post to PHP

Im submitting Data to a php file via AJAX using POST. It worked fine with just submitting strings, but now I wanted to submit my JS Object with JSON and decode it on PHP side.

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

I've 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 alert was just to check everything is alright...

And yea usual string were echoed correctly :-)

Upvotes: 5

Views: 32028

Answers (5)

Cristian Oana
Cristian Oana

Reputation: 1718

On PHP side try this:

$sectionValue = htmlspecialchars($_POST['sectionValue'], ENT_QUOTES);
$dataToWrite = json_decode(html_entity_decode($sectionValue, ENT_QUOTES, "utf-8" ), true);

Upvotes: 0

UltraInstinct
UltraInstinct

Reputation: 44444

Where you went wrong in your code in the first code is that you must have used this:

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

Quoting from 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 submitting a JSON in the body, you have to read it from this stream. Usual method of $_POST['field_name'] wont work, because the post body is not in an URLencoded format.

In the second part, you must have used this:

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

UPDATE:

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

If you must get that using $_POSTyou would make it:

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

And then in PHP do:

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

Upvotes: 23

Rishabh
Rishabh

Reputation: 1195

try this

  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 also do this!

  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);

Upvotes: 0

Flygenring
Flygenring

Reputation: 3848

To me, it looks like you should reformat your AJAX object. The url-property should only be the URL for the target php-file and any data that needs to be posted 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);
    }
  });
}

Upvotes: 0

YemSalat
YemSalat

Reputation: 21486

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

Upvotes: 0

Related Questions