jQuery implements jsonp call

<!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.min.js"></script>
      <script type="text/javascript">
     jQuery(document).ready(function(){
        $.ajax({
             type: "get",
             async: false,
             url: "http://flightQuery.com/jsonp/flightResult.aspx?code=CA1998",
             dataType: "jsonp",
             jsonp: "callback",//The parameter name passed to the request handler or page to get the name of the jsonp callback function (generally the default is: callback)
             jsonpCallback:"flightHandler",//The name of the custom jsonp callback function, the default is the random function name automatically generated by jQuery, you can also write "?", jQuery will automatically process the data for you, jquery automatically generated
             success: function(json ){
                 alert('You have found flight information: fare: ' + json.price + ' yuan, remaining tickets: ' + json.tickets + ' Zhang.');
             },
             error: function(){
                 alert('fail ');
             }
         });
     });
     </script>
     </head>
  <body>
  </body>
</html>



Normal calling method
<head>
    <title></title>
    <script type="text/ javascript">
    // The callback function after getting the flight information query result
    var flightHandler = function(data){
        alert('The result of the flight you queried is: fare' + data.price + 'yuan,' + 'remaining ticket' + data.tickets + 'piece.');
    };
    // The url address that provides the jsonp service (regardless of What type of address is the final generated return value is a piece of javascript code)
    var url = "http://flightQuery.com/jsonp/flightResult.aspx?code=CA1998&callback=flightHandler";
    // Create a script tag and set its Attribute
    var script = document.createElement('script');
    script.setAttribute('src', url);
    // Add the script tag to the head, and call the start
    document.getElementsByTagName('head')[0].appendChild( script);
    </script>
</head>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326939348&siteId=291194637