Convert jsonp to json
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 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.
链接地址: http://www.djcxy.com/p/47504.html上一篇: 如何在打字稿文件中使用json文件
下一篇: 将jsonp转换为json