How Ajax methods in jquery can be invoked remotely via JSONP _jquery

Source: Internet
Author: User
Tags custom name

There are a number of tutorials on the concept of JSONP and why to use JSONP online, and this section mainly demonstrates how Ajax methods in jquery can be invoked remotely via JSONP

First, we introduce the parameters of the next $.ajax
Type: Request Method 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: A parameter name passed to a request handler or page to obtain the name of the JSONP callback function (typically by default: callback)
Jsonpcallback: 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: Calling a successfully executed function
Error: Exception handling function

1. Example 1
Server side we use the MVC action to return the data

Copy Code code as follows:

public class Homecontroller:controller
{
//
Get:/home/

Public ActionResult Index ()
{
Returnview ();
}

Public ActionResult Returnjson ()
{
String callback = request.querystring["Callback"];
String json = "{' name ': ' John ', ' Age ': ' 20 '}";
string result = String. Format ("{0} ({1})", Callback, JSON);
Returncontent (result);
}

}

Clients use JSONP to transmit data
Copy Code code 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 ');
}
});
}
</script>

<input type= "button" value= "submitted" onclick= "SendData ();" />

After clicking the Submit button, the server-side request.querystring["callback" is found to return a random function name. This is set to JSONP format to pass the data.

2. Customize the name of the function
You can customize the function name in the pass process, as long as you use the Jsonpcallback parameter.
JSONP: Represents the passed parameter, the 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.
Copy Code code as follows:

<script type= "Text/javascript" >
Functionsenddata () {
$.ajax ({
Type: "Get",
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 default: 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.