javascript 将 jsonp 转换为 json
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14266570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert jsonp to json
提问by iJade
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.
好的,所以上面的代码工作正常,我得到的数据是 jsonp。现在我不知道如何将 jsonp 转换为 json。
回答by Joshua Burns
This article may give you some additional guidance: Basic example of using .ajax() with JSONP?
这篇文章可能会给你一些额外的指导:使用 .ajax() 和 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);
}
});
回答by Bergi
Now i cant figure out how to convert jsonp to json.
现在我不知道如何将 jsonp 转换为 json。
That's pointless. What you want is a plain javascript object to work with, and you already have that (data
).
那毫无意义。您想要的是一个普通的 javascript 对象,并且您已经拥有了 ( data
)。
JSONPis 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.
JSONP是一个脚本文件,其中使用对象文字调用函数。文字看起来像JSON,函数(其名称是动态生成的)是padding。
JSONis a file/string containing data in JavaScript Object Notation, a common serialisation format.
JSON是一个文件/字符串,其中包含JavaScript Object Notation中的数据,这是一种常见的序列化格式。
回答by Kevin B
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)
,则它已被转换。你应该得到[object Object]
which 应该告诉你你有一个 JavaScript 对象。现在您可以像访问任何其他 JavaScript 对象一样访问它的属性。
alert(data.foo);
It may also be an array depending on the json being returned.
它也可能是一个数组,具体取决于返回的 json。
回答by circusdei
you need a function:
你需要一个功能:
function jsonp(data){
// do stuff with data here
}
the function is called automatically when the data is returned/
返回数据时自动调用该函数/