jquery.ajax Access-Control-Allow-Origin jquery.ajax Access-Control-Allow-Origin xml xml

jquery.ajax Access-Control-Allow-Origin


http://encosia.com/using-cors-to-access-asp-net-services-across-domains/

refer the above link for more details on Cross domain resource sharing.

you can try using JSONP . If the API is not supporting jsonp, you have to create a service which acts as a middleman between the API and your client. In my case, i have created a asmx service.

sample below:

ajax call:

$(document).ready(function () {        $.ajax({            crossDomain: true,            type:"GET",            contentType: "application/json; charset=utf-8",            async:false,            url: "<your middle man service url here>/GetQuote?callback=?",            data: { symbol: 'ctsh' },            dataType: "jsonp",                            jsonpCallback: 'fnsuccesscallback'        });    });

service (asmx) which will return jsonp:

[WebMethod]    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]    public void GetQuote(String symbol,string callback)    {                  WebProxy myProxy = new WebProxy("<proxy url here>", true);        myProxy.Credentials = new System.Net.NetworkCredential("username", "password", "domain");        StockQuoteProxy.StockQuote SQ = new StockQuoteProxy.StockQuote();        SQ.Proxy = myProxy;        String result = SQ.GetQuote(symbol);        StringBuilder sb = new StringBuilder();        JavaScriptSerializer js = new JavaScriptSerializer();        sb.Append(callback + "(");        sb.Append(js.Serialize(result));        sb.Append(");");        Context.Response.Clear();        Context.Response.ContentType = "application/json";        Context.Response.Write(sb.ToString());        Context.Response.End();             }


At my work we have our restful services on a different port number and the data resides in db2 on a pair of AS400s. We typically use the $.getJSON AJAX method because it easily returns JSONP using the ?callback=? without having any issues with CORS.

data ='USER=<?echo trim($USER)?>' +         '&QRYTYPE=' + $("input[name=QRYTYPE]:checked").val();        //Call the REST program/method returns: JSONP         $.getJSON( "http://www.stackoverflow.com/rest/resttest?callback=?",data)        .done(function( json ) {                      //  loading...                if ($.trim(json.ERROR) != '') {                    $("#error-msg").text(message).show();                }                else{                    $(".error").hide();                    $("#jsonp").text(json.whatever);                }        })          .fail(function( jqXHR, textStatus, error ) {        var err = textStatus + ", " + error;        alert('Unable to Connect to Server.\n Try again Later.\n Request Failed: ' + err);        });