Resolve front-End AJAX requests through server-side provisioning cross-domain Access Webservie (C # Development, IIS Publishing)
Problem: Errors cross-domain access error when frontend is called by Ajax
<!--function Description: Ajax call WebService
<script type= "Text/javascript" >
function Test () {
$.ajax ({
Type: "POST",
ContentType: "Application/json;charset=utf-8",
URL: "Http://www.xxxx.org/test.asmx/TestMethod",
Data: "{a:12345,b: ' ABCD '}",
Success:function (response) {
Alert ("Success:" + RESPONSE.D);
},
Error:function (msg) {
Alert ("Error:" + msg);
}
})
}
</script>
Workaround: Add a cross-domain access License in the WebService service profile Web. config file, a total of 2 places to modify
1. Add the system.web node in the configuration node
<!--customer Service cross-domain calls to use--
<webServices>
<protocols>
<add name= "HttpGet"/>
<add name= "HttpPost"/>
</protocols>
</webServices>
Represents a get and post access that can support HTTP, and can be modified with more requirements
2. Add directly to the configuration node
<!--customer Service cross-domain calls to use--
<system.webServer>
<customHeaders>
<add name= "Access-control-allow-methods" value= "Options,post,get"/>
<add name= "Access-control-allow-headers" value= "X-requested-with,content-type"/>
<add name= "Access-control-allow-origin" value= "*"/>
</customHeaders>
</system.webServer>
Where the value of Access-control-allow-origin can be set according to the specific circumstances, "*" means that all sites can access
Resolve front-End AJAX requests through server-side provisioning cross-domain Access Webservie (C # Development, IIS Publishing)