Asqan
Asqan

Reputation: 4489

No 'Access-Control-Allow-Origin' error in cordova

I have a project created by cordova. If i execute the following:

$.ajax({
  url: "http://101.01.105.159/mySource", 
  crossDomain : true,
  beforeSend: function( xhr ) {
    xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
  }
})
  .done(function( data ) {
    if ( console && console.log ) {
      console.log( data);
    }
  });

i take the following error:

GET http://101.01.105.159/mySource n.ajaxTransport.k.cors.a.crossDomain.send @ jquery-2.1.3.min.js:4n.extend.ajax @ jquery-2.1.3.min.js:4(anonymous function) @ VM2064:2InjectedScript._evaluateOn @ VM1791:904InjectedScript._evaluateAndWrap @ VM1791:837InjectedScript.evaluate @ VM1791:693
punten.html:1 XMLHttpRequest cannot load 101.01.105.159/mySource. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 401.

However, if i execute it outside the cordova-environment, it gives the results. What should i do?

Upvotes: 1

Views: 1284

Answers (1)

tqheel
tqheel

Reputation: 11

You need to enable cross-origin resource sharing (CORS) on the web application that you are calling. This varies by platform, but for example, for a Microsoft IIS hosted site, you enable cross origin requests in the web.config file. See http://enable-cors.org/server_iis7.html. Configurations for many other platforms can be found on that same site.

Edit: By "environment" I assume you mean development environment and/or the Cordova app itself. The likely reason that you can access http://101.01.105.159/mySource from outside your "environment" is because you are probably making a plain old http request via a browser. However, the Cordova app is making an AJAX request, which is the specific behavior that would be disallowed when the web application at 101.01.105.159 is not setup to allow CORS.

Upvotes: 1

Related Questions