Safari浏览器ajax请求错误

时间:2013-05-02 12:34:03

标签: jquery html ajax cross-domain

我正在向通用处理程序Handler.ashx发出ajax请求,该处理程序将此请求转发到另一个域中的REST服务。 Handler用于实现跨域调用。 我在Firefox&中获取数据铬。但不适用于Windows 7(版本5.1.7)上的Safari

$.ajax({
         url: 'Handler.ashx',
         type: 'GET',
         contentType: 'application/json; charset=utf-8',
         dataType: 'json',
         async: false,
         timeout: 20000,
         data: data,
         success: function (received_data) {
             // Process data
         },
         error: function (err) {
             console.log(err);
         }
});

我的Handler.ashx代码:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri("http://xxx.xxx.xx.xxx/MyWebService/Service.svc/DownloadFile"));
    req.ContentType = "application/json; charset=utf-8";
    req.Timeout = 60000;

    using (WebResponse resp = req.GetResponse()) 
    { 
        StreamReader reader = new StreamReader(resp.GetResponseStream());
        string responceFromService = reader.ReadToEnd();
        context.Response.ContentType = "application/json; charset=utf-8";
        context.Response.Write(responceFromService);
    }

我得到的错误是:

NETWORK_ERR: XMLHttpRequest Exception 101

3 个答案:

答案 0 :(得分:1)

从我可以在网上浏览的所有内容和我自己的测试中,Safari在10秒后超时同步ajax调用。并且,没有办法解决它。 (我试过了。)这不是一个错误。简直就是Apple向你暗示,如果你的电话在10秒内无法可靠地返回,你不应该使用同步电话。事实上,您可能不应该使用同步呼叫,因为无法知道您的呼叫是否会在10秒内返回。你需要重构你的代码,以便在ajax调用之后发生任何事情,通过回调例程发生,而不是内联。

答案 1 :(得分:0)

尝试将async参数设置为true

$.ajax({
    url: 'Handler.ashx',
    type: 'GET',
    async: true,
});

答案 2 :(得分:0)

我的ajax调用仅在Safari中失败。对我有用的是使用event.preventDefault()

来防止默认的按钮单击行为