We know that Ajax cannot be accessed across domains, but sometimes we do need cross-domain access to get the data, so Jsonp was born with the script tag in its essence, and in addition to JSONP, there was another way to implement cross-domain
First, manually implement JSONP cross-domain
1, first create a Web project, where I use the general handler
1 Public class Demo:ihttphandler 2 {3 public void ProcessRequest (HttpContext context) 4 {5 //Receive parameter 6 String callBack = context. request["CallBack"]; 7 String uName = context. request["UName"]; 8 String data = "({\" name\ ": \" "+ UName +" \ ", \" age\ ": \" 23\ "})"; 9 String josnstr = CallBack + data;10 Conte Xt. Response.Write (JOSNSTR); }12 public bool IsReusable14 { get16 false;18 }19 }20 }
2. Create a new Web project and new HTML file
1 <! DOCTYPE html> 2 3, test, will open two sites, http://localhost:2571: Fill in the first step to create the site address
All two requests return the same information.
Second, add the request header implementation cross-domain
1, the same is the first to create a Web project, the same as the above using a general handler, *.ASHX, here I only post important parts
1 public void ProcessRequest (HttpContext context) 2 {3 //Receive parameter 4 string uName = context. request["UName"]; 5 String data = "{\" name\ ": \" "+ UName +" \ ", \" age\ ": \" 23\ "}", 6 //Simply add the following two sentences 7 context to the server . Response.AddHeader ("Access-control-allow-origin", "*"); 8 //cross-domain can be requested in the way 9 context. Response.AddHeader ("Access-control-allow-methods", "Post,get"); Response.Write (data);
2. Create a new Web project and new HTML file
1 <! DOCTYPE html> 2
}13 }, "JSON") }15 </script>16
3, the last is the test, to see the effect of
Add the following information
Third, Cros realize Webapi cross-domain
1, new WebApi project and through NuGet download package, search package "Microsoft.AspNet.WebApi.Cors", generally I like to download a Chinese package, easy to view comments
2. Add the following code to the Application_Start of global
1 var cors = new Enablecorsattribute ("*", "*", "*"); 2 GlobalConfiguration.Configuration.EnableCors (cors);
Note that these two sentences put Application_Start method at the front, otherwise it will lead to not cross the domain, temporarily do not know the reason
3. Create a new Web project and new HTML file
1 <! DOCTYPE html> 2
4, the final test to see the effect
Add the following information
Summarize1. Create Jsonp cross-domain manually
Pros: No browser required, this method can be used in any browser
Disadvantage: Only get request is supported, the back end of the request is not prompted, which causes the exception to be handled.
2. Add request headers for cross-domain
Pros: Support any request mode, and the backend error will be like non-cross-domain error, you can handle the exception
Cons: Compatibility is not very good, ie words <ie10 do not support this way
Third, I think the principle is the second realization, the actual test found the same as the second, by looking at the head in the request message can also see
Category: WebApiAPI's Cors cross-domain and other cross-domain approaches