gpt4 book ai didi

jquery - 如何使用 jQuery 调用带有 JSON 响应的 ajax 请求?

转载 作者:行者123 更新时间:2023-12-03 23:01:27 24 4
gpt4 key购买 nike

我无法使用以下代码行 jQuery.support.cors = true; 打印成功。包含行 jQuery.support.cors = true; 将给出警告消息。那么如何在不失去功能的情况下避免这种情况呢?我的主要目标是调用返回 JSON 数据的 REST Web 服务,并且我必须利用 JSON 数据。请帮助我如何实现这一目标。请提供工作示例

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.getJSON demo</title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<script>
jQuery.support.cors = true;
$.ajax ({
url: 'http://json-cricket.appspot.com/score.json',
datatype: "json",
success: function (e) {
// Success callback
alert("sucess");
}})
</script>

</body>
</html>

最佳答案

  1. 您可能错过了添加 type//GET 或 POST,REST OPEATION 的类型
  2. dataType 拼写错误
  3. 错过添加contentType
<小时/>
 $.ajax({
type: "POST", //rest Type
dataType: 'jsonp', //mispelled
url: "http://json-cricket.appspot.com/score.json",
async: false,
contentType: "application/json; charset=utf-8",
success: function (msg) {
console.log(msg);
}
});

更新:在试图找出原因时,我认为这是best answer了解问题。

Say you're on domain abc.com, and you want to make a request to domain xyz.com. To do so, you need to cross domain boundaries, a no-no in most of browserland.

The one item that bypasses this limitation is tags. When you use a script tag, the domain limitation is ignored, but under normal circumstances, you can't really DO anything with the results, the script just gets evaluated.

Enter JSONP. When you make your request to a server that is JSONP enabled, you pass a special parameter that tells the server a little bit about your page. That way, the server is able to nicely wrap up its response in a way that your page can handle.

关于jquery - 如何使用 jQuery 调用带有 JSON 响应的 ajax 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18980841/

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