How to remotely call ajax methods in jquery through JSONP

Source: Internet
Author: User
Tags custom name

There are many tutorials on JSONP concepts and why JSONP is used. This section mainly demonstrates how to remotely call ajax methods in JQUERY through JSONP.

First, we will introduce $. ajax parameters.
Type: Request Method GET/POST
Url: request address
Async: boolean type. The default value true indicates whether the request is asynchronous. If it is false, it indicates synchronization.
DataType: returned data type
Jsonp: The parameter name passed to the request handler or page to obtain the name of the jsonp callback function (generally: callback by default)
JsonpCallback: the custom jsonp callback function name. The default value is the random function name automatically generated by jQuery. You can also enter it "? ", JQuery will automatically process the data for you
Success: Calls successfully executed Functions
Error: Exception Handling Function

1. Example 1
On the server side, we use mvc action to return data.
Copy codeThe Code is as follows:
Public class HomeController: Controller
{
//
// GET:/Home/

Public ActionResult Index ()
{
ReturnView ();
}

Public ActionResult ReturnJson ()
{
String callback = Request. QueryString ["callback"];
String json = "{'name': 'zhang san', 'age': '20 '}";
String result = string. Format ("{0} ({1})", callback, json );
ReturnContent (result );
}

}

The client uses jsonp to transmit data.
Copy codeThe 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 ');
}
});
}
</Script>

<Input type = "button" value = "Submit" onclick = "SendData ();"/>

After you click Submit, a random function name is returned for Request. QueryString ["callback"] on the server. In this way, the data is transmitted in JSONP format.

2. Custom function name
You can customize the function name during the transfer process. You only need to use the jsonpCallback parameter.
Jsonp: indicates the passed parameter. The default value is callback. You can also customize the parameter. The server segment uses this parameter to obtain the custom function name. The server obtains the Request. queryString ["callback"]
JsonpCallback indicates the passed parameter value, that is, the callback function name, which is a custom name.
Copy codeThe Code is as follows:
<Script type = "text/javascript">
FunctionSendData (){
$. Ajax ({
Type: "get ",
Async: false,
Url: "/home/ReturnJson ",
DataType: "jsonp ",
Jsonp: "callback", // The parameter name that is passed to the request handler or page to obtain the jsonp callback function name (generally: callback by default)
JsonpCallback: "receive", // custom jsonp callback function name. The default value is the random function name automatically generated by jQuery. You can also enter "? ", 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.