$.ajax call working fine in IE8 and Doesn't work in firefox and chrome browsers

11,727

Solution 1

this is because of the Same origin policy. you cannot use ajax to call external sites. if you really want to use, you have to use JSONP. Or you can use serverside proxy for this. means, call external site in the server side and do ajax call to the that webservice.


UPDATE:

create webserveice in your site and in the webmethod put following code

string proxyURL = "http://devserver:7995/stdpart/services/GetAllPartsWithFilter";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(proxyURL);
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

if (response.StatusCode.ToString().ToLower() == "ok")
{
    Stream content = response.GetResponseStream();
    StreamReader contentReader = new StreamReader(content);         
    return contentReader.ReadToEnd();
}
return string.Empty;

then access local service using your code.

for more information please refer this link

Solution 2

Another solution would be to use the jQuery ajaxTransport extension that uses XDomainRequest for IE8+.

Share:
11,727
Dinesh
Author by

Dinesh

passionate about technology and research

Updated on June 15, 2022

Comments

  • Dinesh
    Dinesh almost 2 years

    here is my code

    $.ajax(
    {
        type: "GET", 
        url: 'http://devserver:7995/stdpart/services/GetAllPartsWithFilter',
        dataType: 'json',
        data: jsonPartsData,
        success: fnGetPartsData, 
        error: PartsLoadError  
    });
    

    This is code working fine in IE8, But getting failed in Firefox and Chrome browsers. When i, inspect the XHR object, it's saying the status code code is 0. I have checked all other questions, none of them are helped me to identify the issue.

    Let me know, if i am doing any thing wrong in this code. If $.ajax has some compatibility issues, then please suggest something equivalent to it.

    Update: We found one solution at http://www.xml.com/pub/a/2005/12/21/json-dynamic-script-tag.html

    It is using the concept of Dynamic Scripting. We have done the same thing in our application, then every thing seems to be working now. Yet to analyze fully.

  • Felix Kling
    Felix Kling over 12 years
    Or use the Access-Control-Allow-Origin header (which is not supported in all browsers though).
  • Dinesh
    Dinesh over 12 years
    Can you please share some sample code, if you have. I Have tried few examples, but nothing worked for me :(
  • Dinesh
    Dinesh over 12 years
    With the pure JavaScript also, i am getting the same issue. I have customized the header by adding Access-Control-Allow-Origin and some other parameters. Nothing worked :(
  • hrishikeshp19
    hrishikeshp19 over 12 years
    Can you confirm that pure JS Ajax DOES work in IE and NOT in chrome/ff/safari ?
  • Chamika Sandamal
    Chamika Sandamal over 12 years
    if you want to use JSONP, you have to change the server code as well. easyest way is using serverside proxy for this. try this link for more details how to use serverside proxy, see the updated answer
  • Chamika Sandamal
    Chamika Sandamal over 12 years
    @Dinesh: is this sample is fine for you? or do i need to update more?
  • Dinesh
    Dinesh over 12 years
    @Sandamal: we are trying the wrapper services as exactly you have suggested.