Scenario: http://localhost:8080/Web App is called cross-domain access when you access an action under http://localhost:8081.
Premise, launch 8080web application instance, and then launch 8081web application instance to use Ajax to access.
First, in your own Web API app, add a NuGet service pack, online:
Then select the installation in the diagram:
Under the app_start/WebApiConfig.cs of the Web API app
public static void Register (Httpconfiguration config) {}
At the end, add a line of code:
Config. Enablecors (New Enablecorsattribute ("*", "*", "*") {Supportscredentials = true, Preflightmaxage = 600});
Then the cshtml or HTML page under 8080 takes advantage of Ajax access:
<script type= "Text/javascript" >
$(function () {
Index_get ();
});
Index_get =function() {
$.ajax ({
URL: "Http://localhost:1687/Api/Console/Index_Get",
Type: "Post",
Data:JSON.stringify ({}),
//dataType: "JSON",//indicates the return value type, does not have to
ContentType: "Application/json;charset=utf-8",//must have, request type
Successfunction(o) {
Alert (o);
},
Errorfunction(o) {
Alert ("Hello, you have done wrong");
},
ProcessData:false
});
}
</script>
ProcessData default value: True. By default, the data option is passed in, and if it is an object (technically, as long as it is not a string), it will be processed into a query string to match the default content type "application/x-www-form-urlencoded". Set to False if you want to send DOM tree information or other information that you do not want to convert.
$.ajax ({
Url:url,
Data:JSON.stringify (data),
Successfunction(data, Textstatus, JQXHR) {
if(typeof(success) = = "function") {
Success (data, Textstatus, JQXHR)
}
},
Errorfunction(JQXHR, Textstatus,errorthrown) {
if(typeof(Error) = = "function") {
Error (Jqxhr,textstatus,errorthrown);
}
},
Xhrfields: {
Withcredentials:true
},
Headers: {
"Authorization": "Platformauth" + App.Setting.AuthToken,
},
ContentType: "Application/json",//request type is in JSON format
Cache: "False",
Type: "POST",
ProcessData:false,//whether to put Ajax in thejson.stringify ({}) converted to? a=f&b=g form
})
ASP-cross-domain access