mike44
mike44

Reputation: 812

Safari browser ajax request error

I'm making ajax request to the generic handler Handler.ashx, which forwards this request to a REST service in another domain. Handler is used to achieve cross-domain call. I get the data in Firefox & Chrome. But not in Safari on Windows 7(Ver. 5.1.7)

$.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);
         }
});

My Handler.ashx code:

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);
    }

The error I get is:

NETWORK_ERR: XMLHttpRequest Exception 101

Upvotes: 0

Views: 7347

Answers (3)

Akhil Mohandas
Akhil Mohandas

Reputation: 212

My ajax call were failing only in Safari. What worked for me was preventing the default button click behavior using event.preventDefault()

Upvotes: 0

user2037235
user2037235

Reputation: 86

From everything I can find surfing around on the web, and my own testing, Safari times out synchronous ajax calls after 10 seconds. And, there is no way to get around it. (I've tried.) This is not a bug. It is simply Apple's way of hinting to you that if your call can't reliably return in 10 seconds, you shouldn't be using a synchronous call. In fact, you probably shouldn't ever use a synchronous call because there is no way to know if your call will return in 10 seconds. You need to refactor your code to have whatever would happen after the ajax call, happen via the callback routine, and not in-line.

Upvotes: 1

Alex
Alex

Reputation: 10226

Try setting the async parameter to true

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

Upvotes: 1

Related Questions