1

I have a string that looks like this

{ "name":"John", "age":30, "city":"New York"}

I want to post it to a PHP file exactly in the format as seen above, not as an array. The reason is that I will later use this string somewhere else, and I have no need to implode it or reformat it.

Here is what I have now.

var sentdata = '{ "name":"John", "age":30, "city":"New York"}';

$.ajax({
    url: 'save.php',
    type: 'post',
    data: sentdata,
    success:function(response){
    console.log('Updated'); 
    }

});

PHP is currently outputting this:

Array ( [{"name":"John","age":30,_"city":"New_York"}] => )

But I wanted a basic string that looks exactly like in sentdata.

I prefer a Jquery/JSON solution, but if I have to do something in PHP then so be it.

Thanks!

pufAmuf
  • 7,415
  • 14
  • 62
  • 95
  • 3
    You need to `JSON.stringify()` the sentdata, and set the contentType: 'json' on the request – Taplar Apr 24 '19 at 15:29
  • 1
    You should receive the data just the way you sent it, post your save.php to see why the data was transformed. – Musa Apr 24 '19 at 15:30
  • 1
    I can hardly imagine where you can use json object as string though. – u_mulder Apr 24 '19 at 15:35
  • @Taplar `sentdata` is already a string and the correct content type is `application/json` – Musa Apr 24 '19 at 15:35
  • @Musa - all I have is $data = $_POST['data']; echo $data; – pufAmuf Apr 24 '19 at 15:37
  • @Taplar — `sentdata`, as Musa points out, is already a string. Running it through `JSON.stringify` will double encode it which is not remotely useful. – Quentin Apr 24 '19 at 15:38
  • Ah, I missed that part. So it would just need the contentType for the request to properly put it in the body. @Quentin – Taplar Apr 24 '19 at 15:40
  • @Taplar — No. The `type` property has to be an HTTP method (like `GET` or `POST`). The `dataType` property can be `json` but it describes the expected response and sets the `Accept` header. To set the Content-Type you need to use the `contentType` property to the correct MIME type (`application/json`) as per my answer. – Quentin Apr 24 '19 at 15:44
  • Mmm, actually that's only the dataType that accepts the 'json' shorthand, -_- – Taplar Apr 24 '19 at 15:44
  • @Taplar You're probably thinking about `dataType` because `http://api.jquery.com/jquery.ajax/` has nothing about `json` for `contentType` – Musa Apr 24 '19 at 15:48

3 Answers3

2

The code should look this, notice the change to JS code

data: {data: sentdata}

var sentdata = '{ "name":"John", "age":30, "city":"New York"}';

$.ajax({
    url: 'save.php',
    type: 'post',
    data: {data: sentdata},
    success:function(response){
    console.log('Updated'); 
    }

});

Then you have string variable in PHP code:

$_POST["data"]
XPS
  • 140
  • 1
  • 5
1

You haven't set the Content-Type of the request, so PHP thinks you are sending application/x-www-form-urlencoded data.

Since your string doesn't have an = in it, it treats this as a key without a value and populates $_POST with that data.

You need to set the Content Type request header explicitly.

$.ajax({
    url: 'save.php',
    contentType: "application/json",
    // etc

However PHP will not automatically process JSON encoded requests. $_POST will be empty.

You need to parse the JSON manually as described in this question.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

You can still send as an object, but add the string as the value itself.

var sentdata = ['data' : '{"name":"John", "age":30, "city":"New York"}'];

Then just access it as $_POST["data"]

imvain2
  • 15,480
  • 1
  • 16
  • 21