5

Here is my code

$.ajax({
    type: "GET",
    url: "http://example.com?keyword=r&callback=jsonp",
    success: function (data) {
        alert(data);
    },
    dataType: "jsonp",
    error: function (xhr, errorType, exception) {
        var errorMessage = exception || xhr.statusText;
        alert("Excep:: " + exception + "Status:: " + xhr.statusText);
    }
});

OK so the above code works fine and i'm getting a data as jsonp.Now i cant figure out how to convert jsonp to json.

iJade
  • 23,144
  • 56
  • 154
  • 243
  • There's no such thing as "json object". JSON is a cross-platform serialized data in the form of a string. It is also different than JSONP. And as @FelixKling said, jQuery will automatically parse it into an object if you did everything correctly. – Fabrício Matté Jan 10 '13 at 20:20
  • 3
    You shouldn't add `&callback=jsonp` to the URL, jQuery will take care of that. Also, if everything works fine, `data` should already be the JS object you need. So, what exactly is your problem? What is `data`? – Felix Kling Jan 10 '13 at 20:21
  • 1
    Read about JSONP: http://stackoverflow.com/questions/3839966/can-anyone-explain-what-jsonp-is-in-layman-terms – Andreas Louv Jan 10 '13 at 20:21
  • Make sure your server is converting your JSON to a JSONP when it's returning the request. It needs to be wrapped in a function call. – Bryan A Jan 10 '13 at 20:23
  • @FelixKling well actually if i dont add a callback to the url it isn't working dats y i appended &callback=jsonp .Its an external url and dats how it will work – iJade Jan 10 '13 at 20:23
  • *"the url it isn't working"* means what exactly? If you add `jsonp: 'jsonp'` as option for `$.ajax`, does it "work" then? We still don't know what the problem with your code is... – Felix Kling Jan 10 '13 at 20:31
  • What does your alert give you *exactly*. – Kevin B Jan 10 '13 at 20:35
  • it gives [object object] as u said – iJade Jan 10 '13 at 20:38
  • In that case, if your json contains a key `"foo"` with a value `"bar"`, `alert(data.foo)` should give you `bar`. – Kevin B Jan 10 '13 at 20:39
  • @jade: Then everything is working fine. You get the response, `data` is an object (or array) and you can access it like any other object/array. Maybe this helps: http://stackoverflow.com/questions/11922383/i-have-a-nested-data-structure-json-how-can-i-access-a-specific-value. Don't use `alert` for inspecting variables, use `console.log`. – Felix Kling Jan 10 '13 at 20:40

4 Answers4

4

This article may give you some additional guidance: Basic example of using .ajax() with JSONP?

Can you provide us with an example of the data structure returned by the request?

In your particular circumstance, you could probably do something similar to the following. Let me know how this turns out:

// Create the function the JSON data will be passed to.
function myfunc(json) {
  alert(json);
}

$.ajax({
  type: "GET",
  url: "http://example.com?keyword=r&callback=jsonp",
  dataType: 'jsonp',
  jsonpCallback: 'myfunc', // the function to call
  jsonp: 'callback', // name of the var specifying the callback in the request
  error: function (xhr, errorType, exception) {
    var errorMessage = exception || xhr.statusText;
    alert("Excep:: " + exception + "Status:: " + xhr.statusText);
  }
});
Community
  • 1
  • 1
Joshua Burns
  • 8,268
  • 4
  • 48
  • 61
  • 1
    so should i remove success from ajax – iJade Jan 10 '13 at 20:29
  • 1
    Actually, JSONP is not JSON at all, it's just JavaScript code. – Felix Kling Jan 10 '13 at 20:31
  • There's no reason to eval in this case... you could simply do `` after defining that function globally. jquery also offers ways of making a same-domain jsonp request with a specific jsonp callback if you must use jQuery to do the request. – Kevin B Jan 10 '13 at 20:33
  • No reason to feel offended or be offensive... just trying to clarify things. Also I'm not sure if `eval(data)` would even work here. I don't think jQuery passes the source of the script to the success callback, because of the way JSONP works (I don't think it's even possible to get the source). – Felix Kling Jan 10 '13 at 20:35
  • @JoshuaBurns eval is not working....its not getting to the jsonp call back function – iJade Jan 10 '13 at 20:48
  • 1
    jQuery is actually smart enough to detect the `callback=jsonp` create inside the URL, create a function with such a name and so that it is called from the script and pass the result to the success callback. See: http://jsfiddle.net/Q42Mb/1/. `eval` doesn't do anything in this case. – Felix Kling Jan 10 '13 at 20:53
  • yup you're right, pretty nifty :D i've updated the code with a better use. – Joshua Burns Jan 10 '13 at 21:56
2

Now i cant figure out how to convert jsonp to json.

That's pointless. What you want is a plain javascript object to work with, and you already have that (data).


JSONP is a script file where a function is called with an object literal. The literal looks like JSON, and the function (whose name is dynamically generated) is the padding.

JSON is a file/string containing data in JavaScript Object Notation, a common serialisation format.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

If you are getting an alert from alert(data), it's already being converted. You should be getting [object Object] which should tell you that you have a JavaScript object. Now you can access it's properties just like any other JavaScript object.

alert(data.foo);

It may also be an array depending on the json being returned.

Kevin B
  • 94,570
  • 16
  • 163
  • 180
0

you need a function:

function jsonp(data){
    // do stuff with data here
}

the function is called automatically when the data is returned/

circusdei
  • 1,967
  • 12
  • 28