First look at an instance
| The code is as follows: |
Copy code |
$ ("# Btn"). click (function (k ){ //... Var j = form. serializeArray (); // serialize name/value $. Ajax ({ Url: "Another domain name/test. php ", DataType: 'jsonp ', Data: j, Jsonp: 'jsonp _ callback ', Success: function (json) {// returned json data Json = json | {}; If (json. msg = 'err '){ Alert (json.info ); } Else if (json. msg = "OK "){ Alert ('submitted successfully '); } Else { Alert ('submission failed '); } }, Timeout: 3000 }) //... });
|
Php section:
| The code is as follows: |
Copy code |
$ Jsonp_callback = $ _ GET ['json_callback']; //... // If correct Echo $ jsonp_callback, '({"msg": "OK "})'; 5. // if an error occurs Echo $ jsonp_callback, '({"msg": "err", "info": "failed to send due to character issues "})';
|
//... It is worth noting that the jsonp method is used, and beforeSend/error cannot be used. Therefore, the verification implemented by js in beforeSend can only be performed by ajax on the server side. php has been verified above.
The domain name contains the following HTML file testjsonp.html:
The code is as follows:
| The code is as follows: |
Copy code |
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <Html xmlns = "http://www.w3.org/5o/xhtml"> <Head> <Title> Untitled Page </title> <Script type = "text/javascript" src = "jquery-1.7.2.min.js"> </script> <Script type = "text/javascript"> JQuery (document). ready (function (){ $. Ajax ({ Type: "GET", Async: false, // Url: "http: // test/jsonp. php", Url: "http://mytaobao.com/jsonp.php", DataType: "jsonp", Jsonp: "callback", // The parameter name passed to the request handler or page to obtain the jsonp callback function name (usually the default value is callback) JsonpCallback: "flightHandler", // custom jsonp callback function name. The default value is the random function name automatically generated by jQuery. You can also write "? ", JQuery will automatically process the data for you Success: function (json ){ Alert ('You found the flight information: fare: '+ json. price +' yuan, remaining ticket: '+ json. tickets +. Callback function name: '+ json. func ); }, Error: function (){ Alert ("fail"); } }); }); </Script> </Head> <Body> </Body> </Html> |
Note: to truly run the above code, you may need jquery files.
| The code is as follows: |
Copy code |
| <Script type = "text/javascript" src = "jquery-1.7.2.min.js"> </script> |
Change the file path of jquery in your Directory:
For example:
| The code is as follows: |
Copy code |
<Script type = "text/javascript" src = "js/jquery. js"> </script>
|
Then, you can find a web directory for another domain name, and set the file jsonp. php:
The code is as follows:
| The code is as follows: |
Copy code |
<? Php $ Callback = $ _ GET ["callback"]; $ A = array ( 'Code' => '000098 ', 'Price' => '123 ', 'Tickets '=> 20, 'Function' => $ callback, ); $ Result = json_encode ($ ); Echo "flightHandler ($ result)"; Exit; |
Put it under this directory. In this way, you can test it.
You can see the effect directly on the browser testjsonp.html.