1

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.

0

1 Answer 1

4

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'
    ....
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much for that information, it helped a lot. You are a lifesaver. I'm not sure how this works but I have to dive more into this subject. Again THANK YOU!
No probs. JSON-P confuses a lot of people early on. I did a detailed blog post on its workings a while back that should help you see what's going on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.