Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery.ajax Access-Control-Allow-Origin

So here is my call:

   $.ajax({
       url: url,
       headers: { 'Access-Control-Allow-Origin': '*' },
       crossDomain: true,
       success: function () { alert('it works') },
       error: function() {alert('it doesnt work')},
       datatype: 'jsonp'
   });

My url is legit. You will notice that i do not have data set. I m not sure if datatype is working properly as its actually xml being returned, but i tried that too. Its a call to sportsdata's api. On the site, they show you a request header of x-originating-ip so i have tried that where access-control-allow-origin is.

All of this still returned the access-control error. I am not clear on what data is if i set it, so i have omitted it for now. I have tried a few different things i googled, i understand why i am getting the error. I do not know how to fix it. I tried to not have to ask, but if someone could explain or show me the way, that would be greatly appreciated

like image 562
dwarf Avatar asked Sep 30 '13 03:09

dwarf


People also ask

How do I fix a CORS issue in AJAX?

Solution. To resolve this error, update your code to make the AJAX call to the new URL provided by the redirect. For more information, see the MDN article CORS request external redirect not allowed.

How do I create a cross origin AJAX request?

Clicking the "Fetch HTML5 Rocks" button generates an Ajax call (via jQuery's $. ajax method) to http://www.html5rocks.com/en/tutorials/file/xhr2/. We set local JavaScript variable article to the contents of the first article found in the returned response and display the contents on our page.

How do I pass Access-Control allow Origin header?

Limiting the possible Access-Control-Allow-Origin values to a set of allowed origins requires code on the server side to check the value of the Origin request header, compare that to a list of allowed origins, and then if the Origin value is in the list, set the Access-Control-Allow-Origin value to the same value as ...

How do you fix CORS missing Allow origin?

If the server is under your control, add the origin of the requesting site to the set of domains permitted access by adding it to the Access-Control-Allow-Origin header's value. You can also configure a site to allow any site to access it by using the * wildcard. You should only use this for public APIs.


1 Answers

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();         
    }
like image 154
Saranya Avatar answered Sep 19 '22 14:09

Saranya