MVC definition Jsonpresult Implementing cross-domain requests1: Principle
in JS, XMLHttpRequest is not able to request data from different domains, but the script tag is possible, so cross-domain requests can be implemented with the script tag. Specifically, define a function, such as jsonp1234, with a function name when requesting a URL for a different domain, for example:http://otherdomain.com/index?callback=jsonp1234, The server then obtains the function name according to callback, and then passes in the JSON string as a function parameter.
2: Implementation
Http://localhost:62203/home/index The page code is as follows
@{ Layout = null;} <! DOCTYPE html> function showmessage (result) { alert ( result.name) } </script> <script src= "Http://localhost:16308/home/index? Callback=showmessage "type=" Text/javascript "></script>
This is the main sentence.
<script src= " Http://localhost:16308/home/index?callback=showMessage " type= "Text/javascript" ></SCRIPT>
You can see that you are accessing a different site, and the parameter value of callback is ShowMessage,
Http://localhost:16308/home/index The code is as follows
Public actionresult Index () { String callback = this. Request["Callback"]; String json= "{\" name\ ": \" Server\ "}"; This. Response.Write (Callback + "(" + JSON + ")"); return new Emptyresult (); }
Gets the function name according to callback, and then takes the JSON string as a function parameter.
Visit Page Http://localhost:62203/home/index , the effect is as follows
Visible, the site localhost:62203 from the site localhost:16308 get the data.
But we look at the implementation of the service side, which is too ugly, but also more troublesome.
Public actionresult Index () { String callback = this. Request["Callback"]; String json= "{\" name\ ": \" Server\ "}"; This. Response.Write (Callback + "(" + JSON + ")"); return new Emptyresult (); }
What we want is to invoke a method that can be implemented across domains, and how to do that. See the controller has a This.json method, the type is Jsonresult, we can refer to this class. Defines a class Jsonpresult, derived from Jsonresult, in which the Executeresult method obtains the function name based on callback, and then passes in the JSON string as a function parameter.
public class Jsonpresult:jsonresult {public static readonly string jsonpcallbackname = "Callback"; public static readonly String callbackapplicationtype = "Application/json"; public override void Executeresult (ControllerContext context) {if (context = = null) { throw new ArgumentNullException ("context"); } if ((Jsonrequestbehavior = = jsonrequestbehavior.denyget) && string.equals (context. HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) {throw new Invalido Perationexception (); } var response = context. Httpcontext.response; if (! String.IsNullOrEmpty (ContentType)) response. ContentType = ContentType; else Response. ContentType = Callbackapplicationtype; if (contentencoding! = null) response. ContentEncoding = this. Contentencoding if (Data! = null) {String buffer; var request = context. HttpContext.Request; var serializer = new JavaScriptSerializer (); if (request[jsonpcallbackname]! = null) buffer = String.Format ("{0} ({1})", Request[jsonpcallbackname], Serializer. Serialize (Data));//First gets the function name according to callback, and then passes in the JSON string as the function parameter else buffer = serializer. Serialize (Data); Response. Write (buffer); } } }
Jsonpresult class has, but want to use THIS.JSONP in this controller, so define an extension method for the Controller class,
Public Static Class controllerextension {public static Jsonpresult JSONP (this controller controller, object data) { Jsonpresult result = New Jsonpresult () { data = data, jsonrequestbehavior = Jsonrequestbehavior.allowget }; return result; } }
This is the controller can directly use the THIS.JSONP, the cross-domain service side of the code to change
Public actionresult Index () { return this. Jsonp (New {name = "Server Jsonpresult" });
It's a lot simpler than the one above.
Open Again Http://localhost:62203/home/index
Similarly, the site localhost:62203 from the site localhost:16308 get the data, and the same as above
MVC definition Jsonpresult Implementing cross-domain requests