gpt4 book ai didi

android - Cordova 没有访问控制允许来源

转载 作者:行者123 更新时间:2023-11-29 23:05:03 24 4
gpt4 key购买 nike

我有一个 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.

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)
}

配置文件

<?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>

最佳答案

您应该将此添加到您的 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>

关于android - Cordova 没有访问控制允许来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56647559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com