同步jquery json请求

发布于 2024-07-23 10:25:57 字数 484 浏览 3 评论 0原文

我需要向last.fm API发出同步请求,但是当我同时使用GET和json时,请求变成异步的。

我的代码:

$.ajax({
      async: false,
    dataType: "json",
    url: "http://ws.audioscrobbler.com/2.0/?method=artist.getimages&artist="+artist+"&api_key="+apiKey+"&format=json&callback=?",
    success: function(html){
          imgURL = html.images.image[1].sizes.size[0]["#text"];
    }
});

如果我删除 dataType: "json" 或使用 POST,它会再次同步,但我依赖于同时使用 json 和 GET。

有任何想法吗?

I need to make a synchronous request to the last.fm API, but when I use GET and json together, the request becomes asynchronous.

my code:

$.ajax({
      async: false,
    dataType: "json",
    url: "http://ws.audioscrobbler.com/2.0/?method=artist.getimages&artist="+artist+"&api_key="+apiKey+"&format=json&callback=?",
    success: function(html){
          imgURL = html.images.image[1].sizes.size[0]["#text"];
    }
});

If I remove dataType: "json" or use POST, it's synchronous again, but I'm dependant on using both json and GET.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幻梦 2024-07-30 10:25:57

啊,这是因为你试图做跨域请求,而跨域请求依赖于动态脚本标签,它永远不可能同步,必须使用数据类型 json 和 GET 方法。

如果您执行 POST 或删除数据类型,您将收到访问错误,因为 same-origin政策。 它将立即返回,但作为失败。

Ah, this is because you're trying to do cross-domain requests, and cross-domain requests rely on a dynamic script tag, which can never be synchronous, must use datatype json and the GET method.

If you do a POST or remove the datatype, you will get an access error because of same-origin policy. It will return immediately, but as a failure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文