A Brief analysis of the cross domain example of jsonp in php
- 2020-06-19 09:50:40
- OfStack
We now have this html file under the name www.test.com:
Note that to actually run the code above may require jquery's file, which you can use < script type="text/javascript" src="jquery-1.7.2.min.js" > < /script > Change the file path to jquery in your directory:
Such as: < script type="text/javascript" src="js/jquery.js" > < /script >
You can then find another web directory for another domain and file jsonp.php:
Put it under this directory. So you can test it.
Access testjsonp. html. directly in the browser to see the effect.
<!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/1999/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",// Passed to a request handler or page for retrieval jsonp The argument name of the callback function name (1 As the default for :callback)
jsonpCallback:"flightHandler",// The custom of jsonp Name of the callback function, default jQuery Automatically generated random function name, can also be written "?" . jQuery It will automatically process the data for you
success: function(json){
alert(' You can check the flight information: Ticket price: ' + json.price + ' Yuan, balance: ' + json.tickets + ' Zhang. The name of the callback function : '+json.func);
},
error: function(){
alert("fail");
}
});
});
</script>
</head>
<body>
</body>
</html>
Note that to actually run the code above may require jquery's file, which you can use < script type="text/javascript" src="jquery-1.7.2.min.js" > < /script > Change the file path to jquery in your directory:
Such as: < script type="text/javascript" src="js/jquery.js" > < /script >
You can then find another web directory for another domain and file jsonp.php:
<?php
$callback = $_GET["callback"];
$a = array(
'code'=>'CA1998',
'price'=>'6000',
'tickets'=>20,
'func'=>$callback,
);
$result = json_encode($a);
echo "flightHandler($result)";
exit;
Put it under this directory. So you can test it.
Access testjsonp. html. directly in the browser to see the effect.