且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用jquery用json响应调用ajax请求?

更新时间:2023-01-16 15:25:49

  1. 您可能会错过添加 type //GET或POST(哪种类型的REST OPERATION)
  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);                
            }
 });

更新: 在试图找出原因时,我认为这是了解问题的***答案.

Updates: While trying to figure out the reason, I think this is the best answer to understand the problem.

假设您在域abc.com上,并且要向域发出请求 xyz.com.为此,您需要跨越域边界,禁止进入 大部分浏览器.

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.

输入JSONP.当您向JSONP服务器发出请求时 启用后,您传递一个特殊的参数,该参数告诉服务器一些信息 关于您的页面的一些信息. 这样,服务器可以很好地打包 其响应以您的页面可以处理的方式.

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.