繁体   English   中英

Cordova中没有“ Access-Control-Allow-Origin”错误

[英]No 'Access-Control-Allow-Origin' error in cordova

我有一个由cordova创建的项目。 如果我执行以下操作:

$.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);
    }
  });

我采取以下错误:

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.

但是,如果我在cordova环境之外执行它,则会得到结果。 我该怎么办?

您需要在要调用的Web应用程序上启用跨域资源共享(CORS)。 这因平台而异,但是例如,对于Microsoft IIS托管站点,可以在web.config文件中启用跨源请求。 请参阅http://enable-cors.org/server_iis7.html 可以在同一站点上找到许多其他平台的配置。

编辑: “环境”是指开发环境和/或Cordova应用程序本身。 您可以从“环境”外部访问http://101.01.105.159/mySource的可能原因是因为您可能正在通过浏览器发出简单的旧http请求。 但是,Cordova应用程序正在发出AJAX请求,当101.01.105.159处的Web应用程序未设置为允许CORS时,这是特定的行为。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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