Get json data from php file

Good morning,

I'm just a beginner in json, I have generated data (array) encoded and sent from a php file, the only thing I want is how to get this data and warn that it is simple.

My object sent from the php file looks like this: {"stat":"opened","do":"close"} I found a better way to do this, var obj = jQuery.parseJSON(???)but really can't make it work, and Google doesn't want to help me this time!

Edit: json object received from response to post: $.post("page.php",{},function(data){/*Here I should pars data and act with*/});

Very happy if you support me in this release! Regards.

+3
source share
3 answers

jQuery, JSON $.parseJSON(). - .

var obj = jQuery.parseJSON('{"stat":"opened","do":"close"}');
console.log(obj.stat, obj.do);
+2

JSON JavaScript jQuery.parseJSON().

jQuery, , dataType jQuery.post() (docs).

,

jQuery.post(
    "file.php",
    function (data) { /* data is a JS object already-parsed */ },
    "json"  // <-- tell jQuery that we expect a JSON response
);

Content-Type PHP , jQuery ( , script), JSON application/json.

+2

In your php file, you should also set header ('Content-type: application / json');

0
source

All Articles