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!

10
  • 3
    You need to JSON.stringify() the sentdata, and set the contentType: 'json' on the request Commented Apr 24, 2019 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. Commented Apr 24, 2019 at 15:30
  • 1
    I can hardly imagine where you can use json object as string though. Commented Apr 24, 2019 at 15:35
  • @Taplar sentdata is already a string and the correct content type is application/json Commented Apr 24, 2019 at 15:35
  • @Musa - all I have is $data = $_POST['data']; echo $data; Commented Apr 24, 2019 at 15:37

3 Answers 3

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"]
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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"]

2 Comments

Uncaught SyntaxError: Malformed arrow function parameter list
Not correct js syntax, it should be var sentdata = {data: '...'}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.