einar einar - 6 months ago 21
Javascript Question

JSON and JS Callback function

I'm having some problems understanding JS Callbacks in regards to getting and parsing JSON information.

What I'm trying to do is using this remote code to populate a select dropdown.

setCategories({
"categories": ["Billing", "Gameplay", "Bugs", "Rules & Policies", "Technical Support"]
});


This code here above is coming from a remote .js file, this is the whole content of the file. I can't link to the file at this point.

Now I'm using what I know to access the JSON information

$.ajax({
url: "http://web.ccpgamescdn.com/common/frontendtest/categories.js",
dataType: 'jsonp',
data: data,
success: function(data, textStatus, jqxhr) {
console.log(data); //data returned

}
});


How ever this returns me this error message from the console


Uncaught ReferenceError: setCategories is not defined


I know how to parse a simple json file, but this JS Callback is beyond my knowledge and I'm not sure how to work with this.

Answer

Does the function setCategories actually exist? If not, or if it's not global, that's your problem.

With jQuery it's better to let jQuery manage the name of JSON-P callbacks, but for cases where the web service is inflexible regarding the name of the callback function it expects, you can accommodate this via the jsonpCallback param when building your AJAX request:

$.ajax({
    ....
    jsonpCallback: 'setCategories'
    ....
});
Comments