我有一个webapi项目(.net mvc webapi),该项目返回一些包装在json / xml中的数据,因此我的cordova项目将请求发送到特定的api地址以获取数据,但是我收到访问错误
在以下位置访问XMLHttpRequest 原产地的“ https://06ae934f.ngrok.io/api/Catalog/get_catalogs” “空”已被CORS政策阻止:否 请求中存在“ 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>
答案 0 :(得分:1)
您应该将其添加到webApi项目中的web.config中:
<script>
$(document).ready(function(){
var file=$('#flFile');
var dvClose=$('#dvClose');
dvClose.hide();
file.click(function(){
dvClose.show();
});
$('#close').click(function(){
file.val('');
dvClose.hide();
});[enter image description here][1]
});
</script>
<div class="input-group">
<div class="custom-file">
<input type="file" id="flFile">
<div id='dvClose'><label style='padding-top: 7px;' class="" for="">
<span id='close' class='fa fa-close'></span></label></div>
</div>
<div class="input-group-prepend">
</div>
</div>