简体   繁体   English

科尔多瓦没有访问控制允许来源

[英]cordova No Access-Control-Allow-Origin

i have a webapi project(.net mvc webapi) that returns some data wrapped in json/xml so my cordova project send request to the certain api address to get the data but i get an access error as我有一个 webapi 项目(.net mvc webapi),它返回一些包装在 json/xml 中的数据,所以我的 Cordova 项目向某个 api 地址发送请求以获取数据,但我得到一个访问错误

Access to XMLHttpRequest at ' https://06ae934f.ngrok.io/api/Catalog/get_catalogs ' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.从源“null”访问“ https://06ae934f.ngrok.io/api/Catalog/get_catalogs ”处的 XMLHttpRequest 已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头.

js code js代码

const Http = new XMLHttpRequest();
const url='https://06ae934f.ngrok.io/api/Catalog/get_catalogs';
Http.open("GET", url);
Http.send();

Http.onreadystatechange = (e) => {
  alert(Http.responseText)
}

config.xml配置文件

<?xml version='1.0' encoding='utf-8'?>
<widget id="org.test.test" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>Catalog</name>
    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="dev@cordova.apache.org" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <plugin name="cordova-plugin-whitelist" spec="1" />
    <access origin="*" />
    <allow-navigation href="*"/>
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <access origin="https://06ae934f.ngrok.io"/>
    <platform name="android">
        <allow-intent href="market:*" />
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
    </platform>
</widget>

You Should Add This to web.config in your webApi project :您应该在 webApi 项目中将其添加到 web.config 中:

<system.webServer>
   <httpProtocol>
      <customHeaders>
          <add name="Access-Control-Allow-Origin" value="*" />
          <add name="Access-Control-Allow-Headers" value="Content-Type" />
          <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
    </customHeaders>
   </httpProtocol>
</system.webServer>

Answer i use cordova and i have no web.config file in the list.回答我使用cordova,但列表中没有web.config 文件。

<content src="index.html" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
    <icon src="res/android/icon.png" />
    <allow-intent href="market:*" />
    <resource-file src="resources/android/xml/network_security_config.xml" target="app/src/main/res/xml/network_security_config.xml" />
</platform>
<platform name="ios">
    <allow-intent href="itms:*" />
    <allow-intent href="itms-apps:*" />
</platform>
<system.webServer>
    <httpProtocol>
        <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*" />
            <add name="Access-Control-Allow-Headers" value="Content-Type" />
            <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
        </customHeaders>
    </httpProtocol>
</system.webServer>

but nothing keeps giving me that error但没有什么总是给我那个错误

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

相关问题 webview-访问控制允许原点不允许原点(使用cordova将angular转换为mobile) - webview - origin is not allowed by access-control-allow-origin (convert angular to mobile using cordova) android webview - 访问控制允许来源 - android webview - Access-Control-Allow-Origin Angular 无“访问控制允许来源” - Angular No 'Access-Control-Allow-Origin' Phonegap / Cordova App在Jelly Bean中断 - Access-Control-Allow-Origin和setAllowUniversalAccessFromFileURLs - Phonegap/Cordova App breaks in Jelly Bean - Access-Control-Allow-Origin and setAllowUniversalAccessFromFileURLs ionic:请求的资源上不存在“ Access-Control-Allow-Origin”标头 - ionic: No 'Access-Control-Allow-Origin' header is present on the requested resource Android 4.1上的Access-Control-Allow-Origin错误 - Access-Control-Allow-Origin Error At Android 4.1 WebView不存在“ Access-Control-Allow-Origin”标头[ANDROID] - WebView No 'Access-Control-Allow-Origin' header is present [ANDROID] 离子怪异的“访问控制允许来源”问题 - Ionic weird 'Access-Control-Allow-Origin' issue Android webview - XMLHttpRequest无法加载Origin <url> Access-Control-Allow-Origin不允许使用 - Android webview - XMLHttpRequest cannot load Origin <url> is not allowed by Access-Control-Allow-Origin Access-Control-Allow-Origin不允许在PhoneGap中使用ng-views获取Origin - using ng-views in PhoneGap getting Origin is not allowed by Access-Control-Allow-Origin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM