Mark Lawrence
Mark Lawrence

Reputation: 355

Accessing/decoding JSON data

Rephrasing this question.

  1. User clicks on a link which takes them to editUser.php?id=1 (where ID is the users ID number)
  2. The editUser.php page on document ready calls the function loadAjaxVals(script, json_action, id);. This function is located in an external JS library. The script and json_action values aren't relevant to this question. The ID is the ID from the URL query string
  3. The loadAjaxVals() sends a request to a PHP script which returns the data to loadAjaxVals(script, json_action, id) as json_encoded() data
  4. loadAjaxVals() takes the json encoded data and loops through it outputting the results to a form on editUser.php. The script takes the name value pairs from the JSON encoded data which become form element IDs and values
  5. The form is populated and were all done.

However I want a second script located on editUser.php to parse the JSON and extract 1 piece of data from it.

I dont know how to access the JSON or parse the JSON.

Upvotes: 0

Views: 105

Answers (2)

gen_Eric
gen_Eric

Reputation: 227240

The JSON is only available in the loadAjaxVals function. I suggest you edit the loadAjaxVals to accept a callback function.

Something like this:

function loadAjaxVals(script, json_action, id, callback){
    // get JSON
    // code
    // parse JSON
    // more code
    if($.isFunction(callback)){
        callback(data); // pass callback the parsed JSON data
    }
}

Then call it like:

loadAjaxVals(script, json_action, id, function(data){
    // use data
    console.log(data);
});

Upvotes: 1

Samy Vilar
Samy Vilar

Reputation: 11130

Well you can use the 'in' operator to check if an object has an attribute ...

alert('enable_class' in obj)

should be true if indeed that object hast enbable_class either wise false

As for your second question, it may be possible that are you are using a cache version of the object, this is quite common, trust me, what I would recommend is clearing your cache and trying again, I also recommend Google Chrome javascript debugger which is built in and comes along with a very nice console and host of other niceties ... :)

If indeed the problem is the cache their are a couple ways around it ... I'll elaborate once you've confirmed it.

Upvotes: 0

Related Questions