This section mainly demonstrates how the AJAX approach in jquery can be invoked remotely via Jsonp, and friends who need it may refer to the following
There are many tutorials on the concept of JSONP and why to use JSONP online, this section mainly demonstrates how Ajax methods in jquery can be invoked remotely via JSONP first introduce $.ajax parameters Type: Request mode get/post URL: Request Address Async: Boolean type, default to True to indicate whether the request is asynchronous, or false for synchronization. DataType: Returned data type JSONP: The parameter name that is passed to the request handler or page to obtain the JSONP callback function name (generally default: callback) Jsonpcallback: Custom JSONP callback function name, default to jquery automatically generated random function name, also can write "?", jquery will automatically process data for you success: Invoke successfully executed function Error: Exception handling function 1. Example 1 Server side we use the MVC action to return the data code as follows: public class HomeController: controller { // //Get:/home/ public ActionResult Index () { Returnview (); &nbs P } public ActionResult Returnjson () { String callback = request.querystring["Callback"]; String json = ' {' name ': ' John ', ' Age ': ' '} '; string result = String. Format ("{0} ({1})", Callback, JSON); returncontent (Result); } } clients use JSONP to transmit data & nbsp The code is as follows: @{ viewbag.title = "Index"; Layout = "~/viewS/shared/_layout.cshtml "; } <script src=" ~/scripts/jquery-1.7.1.min.js "type=" Text/javascript "> </script> <script type=" Text/javascript "> functionsenddata () { $.ajax ({ Type: "Get", async:false, URL: "/home/returnjson", dataType: "Jsonp", success:function ( Data) { alert (data.name); }, error:function () { alert (' fail '); } }); } & lt;/script> <input type= "button" value= "submitted" onclick= "SendData ();" /> Click the Submit button to discover that the server-side request.querystring["callback" returns a random function name. This is set to the JSONP format to pass the data. 2. Custom Function name You can customize the name of functions in the process of passing, as long as you use the Jsonpcallback parameter. JSONP: For passing parameters, default is callback, we can also customize, the server segment through this parameter, get the custom function name, the server obtains request.querystring["callback"] Jsonpcallback represents the parameter value passed, which is the function name of the callback, which is the custom name. Code as follows: <script type= "Text/javascript" > functionsenddata () { $.ajax ({ type: "Get", & nbsp Async:false, URL: "/home/returnjson", dataType: "Jsonp", Jsonp: "Callback",//passed to the request handler or page to obtain the parameter name of the JSONP callback function name (General defaults to: callback) Jsonpcallback: "Receive",//Custom JSONP callback function name, default to jquery automatically generated random function name, also can write "?", jquery will automatically process the data for you Success:function (data) { alert (data.name); }, error:function () { alert (' Fail '); } }); } functionreceive (data) { alert (data.age); } </script>