1

我目前有一个像这样设置的 jQuery ajax 请求

$(document).ready(
    function() { $.ajax({ 
        url : "http://www.my-computer.com:51000/getJson",
        cache : false,
        dataType: "json",
        success : renderPage,
        error: handleError
    })
});

如果我手动导航到http://www.my-computer.com:51000/getJson,我会看到它正确返回了一个 json 字符串,但是对于上述请求,我总是陷入“handleError”方法,文本状态为“错误”并没有太多有用的信息。任何人都可以帮忙吗?谢谢!

编辑:对不起我的电脑域。这个stackoverflow提交不会让我输入localhost,所以我输入了一个任意域。我试过萤火虫,但没能把 json 找回来。

4

2 回答 2

0

Have you tried adding clientaccesspolicy.xml file to the root with the following in it?

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
  <cross-domain-access>
    <policy>
      <allow-from http-request-headers="*">
        <domain uri="*"/>
      </allow-from>
      <grant-to>
        <resource path="/" include-subpaths="true"/>
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>

I am also using a div to display any error messages in, for debugging purposes, as that might help you to see what error message in in the JSON String:

function GetShoppingCartData() {
    jQuery.ajax({
        type: "POST",
        url: "DesktopModules/EcomDnnProducts/AjaxProductDisplay.aspx/GetShoppingCartData",
        data: "{'CartId': '" + jQuery(".shoppingcartid").attr("value") + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        cache: false,
        success: function (msg) {
            buildShoppingCart(msg.d);
        },
        fail: function (msg) {
            jQuery('#productattributesdata').text(msg.d);
        }
    });
}

In my .net code, I had to add the following to the web.config file:

    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
  <system.web>

hth

于 2012-09-04T03:23:00.060 回答
-3

你的代码坏了。请注意,您只有一个以上{}您编码的方式都是错误的。另外,您要从哪个域发送此请求?如果您从 my-computer.com 以外的域发送它,那么您的请求将失败。如果没有,试试这个:

$(文档).ready(函数(){
$.ajax({

        网址:“http://www.my-computer.com:51000/getJson”,
        缓存:假,
        数据:“json”,
        成功:渲染页面,
        错误:句柄错误
    })
});
于 2012-09-04T00:28:25.057 回答