JSONP accessing data across domains

Source: Internet
Author: User

Example one, using the $.getjson method of jquery

Front-end JS:

$ ("#selId"). Click (function () {
$.Getjson("Http://www.example.com/remote.php?callback=?", Callback=? Parameters are focused and will be passed to the background for automatic processing
{name: ' max ' Age: 23 Str: ' hello ' },
function (data {
console.logdata }
);

});
/span>

//back-end remote.php

$callback = $_request[' callback '); The front desk passed it over? is automatically parsed into a function name by jquery.
$arr=Array(' Name '=' Hello ', ' age ' =, ' male ' = ' female ', ' str '= >$_request[' str '];
$tmp = json_encode($arr);
echo $callback." ($tmp) "; //In essence, backstage here just need to return "Funname ({name: ' Max ', age:23} JSON data)" in the format of the string is all Right

Example two, using the $.ajax method of jquery

Front-End Code:

$ ("#selId"). Click (function () {
$. Ajax({
Type: "GET",

The Asyn default is true, which means that an asynchronous request is used; false means that a synchronization request is used, the synchronization request locks the browser, and the user's other actions wait for the request to complete before they can execute
async: false
url: " Http://www.example.com/remote.php ", // Cross-domain background files, mainly generated strings;
datatype: "JSONP" Span class= "pun" >, //get
Jsonp jsonp method : , //is used to obtain the name of the JSONP callback function name, generally callback 

/** a custom Jsonp callback function name, at which time the background return string is echo "Remotehandler (JSON)"; jquery is randomly generated automatically, and the backend code is $callback =$_request[' callback ', Echo $callback. Jsonstr.

*/
Jsonpcallback: "Remotehandler",
Success: function(json) {
Console. Log(json);
},

});

});

Backstage remote.php

$arr = Array (' name ' = = ' Hello ', ' age ' =>23, ' male ' = ' female ', ' str ' =>$_request[' str ');

$tmp = Json_encode ($arr);
echo "Remotehandler ($tmp)"; // Remotehandler with the front door jsonpcallback: "Remotehandler" Correspondence

JSONP accessing data across domains

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.