gpt4 book ai didi

jquery - 从远程域检索 JSON,jQuery

转载 作者:行者123 更新时间:2023-12-01 05:01:52 25 4
gpt4 key购买 nike

我正在尝试在 jQuery 脚本中从远程域(Zencoder 视频编码 API)检索 JSON 格式的数据。当脚本运行时,它返回代码 200,但 Firebug 在地址上显示错误(粗体红色突出显示,带有 X 的圆圈),并且响应为空。

这是与此相关的 jQuery 脚本:

var checkStatus = function(jobID) {
var url = 'https://app.zencoder.com/api/v2/jobs/' + jobID + '/progress.json?api_key=asdad3332d';
$.getJSON(url, function(data) {
if (data.state == 'processing') {
//Do things to indicate job is still going
//Repeat this function to check status again
setTimeout(function() {
checkStatus(jobID);
}, 6000);
} else if (data.state == 'finished') {
//Do some stuff
} else if (data.state == 'failed') {
//Show errors, do other stuff
}
});
};

这是返回的 JSON 示例。

{"outputs":[{"id":18492554,"state":"finished"},{"id":18492553,"state":"finished"},{"id":18492555,"state":"finished"}],"input":{"id":12437680,"state":"finished"},"state":"finished"}

最后,这是 Firebug 返回的响应 header :

Response Headers
Cache-Control private, max-age=0, must-revalidate
Connection close
Content-Length 174
Content-Type application/json; charset=utf-8
Date Thu, 09 Feb 2012 16:06:13 GMT
Etag "48f2d50a838e0e1e433f7c0ba197e787"
Server ZenServer 0.1.0
X-Zencoder-Rate-Remaining 4999

如有任何帮助,我们将不胜感激。对此我摸不着头脑

文档这是指获取作业进度的 API 文档,这正是我想要做的。 https://app.zencoder.com/docs/api/jobs/progress

最佳答案

您需要使用 jsonp 进行跨域请求,并且服务器需要能够接受 jsonp 请求并做出适当的响应。

$.ajax({
url: 'https://app.zencoder.com/api/v2/jobs/1234/progress.js?api_key=asdf1234',
dataType: 'jsonp',
success: function(data){// your code here
}
});

jQuery 将自动附加 callback=blah 参数。请参阅此处的数据类型部分:http://api.jquery.com/jQuery.ajax/

成功函数中的data参数将包含该对象(不仅仅是JSON字符串,而是由JSON字符串表示的对象)。

关于jquery - 从远程域检索 JSON,jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9214518/

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