ASP-cross-domain access

Source: Internet
Author: User


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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.