2

I'm using jstree on a project and attempting to save my tree to a database.

I'm obtaining the tree data as follows:

var tmp = $('#tree').jstree(true).get_json();
console.log(tmp);

This produces a JSON object in the console as I'd expect:

enter image description here

However when I post this to a PHP script using jquery...

 $.ajax({
    type: 'POST',
    url: '/saveTree',
    data: {'tree': tmp},
    success: function(msg) {
      console.log(msg);
    }
  });

... It is showing a PHP array of my data:

enter image description here

The script which I have at /saveTree displays the POST data in the tree array post key:

var_dump($this->request->data['tree']);

I assumed since the data I'm posting to the script is in JSON format I'd need to json_decode() it at the other end? If not, why not?

I've also tried adding dataType: 'json', in the ajax request but that makes no difference.

What's happening here?

Please note the PHP script at /saveTree is running in CakePHP 2.x so the line of PHP above is equivalent to var_dump($_POST['tree']) in regular PHP.

7
  • 1
    Cake will automatically decode JSON when the incoming request has the proper Content-Type header. Commented Nov 27, 2017 at 14:34
  • Ok... do you mean if I put dataType: 'json' in the ajax request? Because it seems to be doing exactly the same thing with or without that. That's why I was confused! Commented Nov 27, 2017 at 14:37
  • Furthermore Content-Type:text/html; is showing in Chrome's Network tab when making the request to /saveTree Commented Nov 27, 2017 at 14:40
  • Content-Type:text/html on the request, or the response? Commented Nov 27, 2017 at 14:45
  • Ah my bad, i was looking at the Response. The Request is application/x-www-form-urlencoded; charset=UTF-8 irrespective of whether dataType has been specified in the ajax call. Is that normal? Commented Nov 27, 2017 at 14:47

1 Answer 1

2

If you want send the data as string you can JSON.stringify(tmp);

 tmp = JSON.stringify(tmp);

 $.ajax({
    type: 'POST',
    url: '/saveTree',
    data: {'tree': tmp},
    success: function(msg) {
      console.log(msg);
    }
  });
Sign up to request clarification or add additional context in comments.

9 Comments

I want to send it as JSON. It appears as JSON when I do the initial console.log. Please have a read over the comments (including chat) on the original post. Something weird is going on because it's being "changed" from JSON to some kind of form data representation and I cannot understand why. The original JSON in the first console.log is what I want...and then to json_decode() it in PHP on the receiving end?
Do you want to receive it as a string on php? or associative array?
If you send it on ajax on a json format, you will receive that on php as a an associative array.
Ok, in that case it works according to my needs I guess. I didn't understand this initially so thanks for clarifying it. I thought that because the data was in JSON format during the request, it had to be json_decode()'d at the other end for PHP to get it into an array. If PHP does this automatically that's great.
You will only need to json_decode if you convert it to string (Like my asnwer) and send it.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.