cordova无访问控制-允许-来源

pes8fvy9  于 2022-11-15  发布在  其他
关注(0)|答案(2)|浏览(159)

我有一个webapi项目(.net mvc webapi),它返回一些封装在json/xml中的数据,因此我的cordova项目向某个api地址发送请求以获取数据,但我得到一个访问错误,如
CORS策略已阻止从源“null”访问“https://06ae934f.ngrok.io/api/Catalog/get_catalogs”处的XMLHttpRequest:请求的资源上不存在“Access-Control-Allow-Origin”标头。
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>
pzfprimi

pzfprimi1#

您应该在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>
bttbmeg0

bttbmeg02#

回答:我使用的是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>

但没有什么能一直让我出错

相关问题