How Ajax methods in jquery can be invoked remotely via JSONP

Source: Internet
Author: User
Tags exception handling json custom name

  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> 
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.