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.
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);
}
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With