简体   繁体   中英

CORS request using jQuery - $.ajax()

I am developing a web application where data is coming from different domain. I mean in my application almost 90% request are cross domain request.

I am unable to get the data while deploy this application on IIS.

Server is deployed on http://some.ip.add/crmservice client is deployed on http://diffent.ip.add/saascrm

I am using jQuery 2.0 to get the data in async manner using $.ajax();

Note: data is coming in xml format

Added some stuff to web.config file also.

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

This is my snippet.

$.support.cors = true;
      $.ajax({
                        type: "GET",
                        url: 'http://some.ip.add/crmservice/crmservice.asmx/HandShake', 
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        crossDomain: true,
                        beforeSend: function (request) {
                        //    debugger;
                            request.setRequestHeader("Access-Control-Allow-Origin", '*');
                        },
                        error: function (xhr, status, error) {
                            try {
                              //  debugger;
                                // debugger;
                              //Here i am getting error : Access denied in IE 9.0 and and just "error" in firefox. 
                                var msg = JSON.parse(xhr.responseText);
                                alert(msg.Message);
                            }
                            catch (e) {
                                // debugger;
                                alert(xhr.statusText);
                            }
                            return true;

                        },
                        success: function (data) {
                            debugger;
                            xmlDoc1 = $.parseXML(data.d);
                            $xml1 = $(xmlDoc1);
                            if ($xml1.find('Result').text() == '0') {
                                $(this).MessageBox('success', $xml1.find('Message').text());
                                $("#uxDBName").prop("disabled", false);
                                $("#uxSUPassword").prop("disabled", false);
                                $("#uxServiceURL").prop("disabled", true);
                                GetListOfB1Databases(url);
                            }
                        }
                    });

my server code is :

Global.asax

protected void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        HttpContext.Current.Response.Cache.SetNoStore();
        EnableCrossDmainAjaxCall();  
    }
    private void EnableCrossDmainAjaxCall()
    {
        HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Origin", "*");

        if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Methods", "GET, POST");
            HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Headers","Content-Type, Accept");
            HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Origin", "*");
            HttpContext.Current.Response.AppendHeader("Access-Control-Max-Age", "1728000");
            HttpContext.Current.Response.End();
        }
    }

     //Web method
     [ScriptMethod(ResponseFormat = ResponseFormat.Json), WebMethod(EnableSession = true)]
    public string HandShake()
    {
        return General.Response("0", "Tenant is in reachable. Please specify SAP Business One Company database\r\nand 'manager' Password", "");
    }

I found some solutions on this also, I found that CORS is not supported by IE 8 & 9. IE 8 * 9 does not create the instance of XMLHttpRequest object. It create XDomainRequest, so need to check for the user agent. I found an alternate solution here

Now my problem is I have use $.ajax() method everywhere almost 90% call is cross domain call. I don't want to make this major change in my framework.

Is there any solution to this using $.ajax()?

Please help me, I am badly stuck since a week.

Thanks in advance.

Thanks for all of your cooperation and help that you provide me. I found solution.

  var url = $("#uxServiceURL").val(); $.ajax({ crossOrigin: true, url: url + '/HandShake', error: function (xhr, status, error) { try { alert('Error'); } catch (e) { alert(xhr.statusText); } return true; }, success: function (data) { var d1 = data.replace(/\\&lt;/g, '<').replace(/\\&gt;/g, '>') xmlDoc1 = $.parseXML(d1); $xml1 = $(xmlDoc1); if ($xml1.find('Result').text() == '0') { $(this).MessageBox('success', $xml1.find('Message').text()); } } }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM