So, through AJAX, you sent a request to another PHP page that process and return some JSON format response like the following:

{
    "filename":"my_uploaded_file.png",
    "url":"QWwYjb7e8dem2Jh9jBnwKKUMoNud1eUHOYyCYtkDUqkalD1tJvJasCnJT6ucuHlcnLNldw0hTDpFJS7nyAdYKHy5CwUVyK6Ufms2",
    "status":"New file uploaded."
}

So, how are we suppose to parse this JSON in jQuery? Normally, a typical jQuery AJAX request will be as followed:

$.ajax({
    url: "test.html",
}).done(function(json){
    // some function here
});

So, we will want to make sure that the JSON is parsed in done function. To parse the JSON from PHP, we need to get the data from the json variable and after that we can access it just like an array.

var obj = jQuery.parseJSON(json["responseText"]);
// access the filename value from JSON
alert(obj["filename"]);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s