gpt4 book ai didi

jquery - 如何将 jsonp 解析为 javascript 对象?

转载 作者:行者123 更新时间:2023-12-01 04:49:20 24 4
gpt4 key购买 nike

我正在使用返回 jsonp 字符串的跨域 api。我想将它解析为一个 javascript 对象,以便更容易使用。我知道使用 json 字符串你可以这样做:

  success: function (val) {
var result = JSON.parse(val);
}

但是如果我使用从 api 获得的 jsonp 执行此操作,我会收到“Uncaught SyntaxError: Unexpected token o”

我做错了吗,还是这不是 jsonp 的方法?

--------编辑1----------------------------如果我打开它,这就是我的 jsonp 字符串的样子:

Object {resource: "boxscore", parameters: Object, resultSets: Array[22]}
parameters: Object
resource: "boxscore"
resultSets: Array[22]
0: Object
1: Object
2: Object
3: Object
4: Object
headers: Array[28]
0: "GAME_ID"
1: "TEAM_ID"
2: "TEAM_ABBREVIATION"
3: "TEAM_CITY"
4: "PLAYER_ID"
5: "PLAYER_NAME"
6: "START_POSITION"
7: "COMMENT"
length: 28
__proto__: Array[0]
name: "PlayerStats"
rowSet: Array[26]
0: Array[28]
0: "0041300201"
1: 1610612764
2: "WAS"
3: "Washington"
4: 2772
5: "Trevor Ariza"
6: "F"
7: ""
8: "37:20"
9: 7
10: 10
11: 0.7
12: 6
1: Array[28]
2: Array[28]
3: Array[28]
4: Array[28]
5: Array[28]
6: Array[28]

所以我想做的是用每个数组中的 header 信息解析数据,我该怎么做?例如,如果我想要 GAME_ID,我只需编写 GAME_ID,然后我会得到每个数组的 gameid“0041300201”。

最佳答案

你可以使用json库..,如果跨域站点给出json字符串那就太好了:

go to this link it may be helpful.

http://www.jquery4u.com/function-demos/jsonp/

or you can use the code like


/* Loading JSON objects using JSONP */
(function($) {
var url = 'http://www.jquery4u.com/scripts/jquery4u.settings.json';
$.ajax({
type: 'GET',
url: url,
async: false,
contentType: "application/json",
dataType: 'jsonp'
});
})(jQuery);

关于jquery - 如何将 jsonp 解析为 javascript 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23503317/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com