Native ajax request and jsonp cross-domain processing

<script>
         // var xhr = new XMLHttpRequest (); 
        // xhr.open ('get', 'http: //api.map.baidu.com/location/ip? ak = 0Ow2Wf7p5ypNupzzFKwy3NQCz2FFlMa9 & coor = bd09ll'); 
        // xhr.send () // get request; 
        // 1.post request sets the request header; 
        // 2. Put the carried parameters in send eg: xhr.send (a = 1 & b = 0) 
        // xhr.onreadystatechange = function () { 
        //      if (xhr.status == 200 && xhr.readyState == 4) { 
        //          console.log (xhr.responseText); 
        //      } 
        // } 

        // jsonp (only for get requests) resolve cross-domain Question 
        / * 
        1. Create a script tag dynamically; 
        2. Register a global function to receive data; 
        3. Modify the src attribute value of the script to the interface that needs to be requested;
        4. Splice a callback function at the back of the street, callback = the global function name registered above;
        5. Put the dynamically created script tag into the body 
        
        * / 
        function test (data) { 
            console.log (data); 
        } 
        document.onclick = function () {
             var script = document.createElement ('script' ) 
            script.src = 'http: //api.map.baidu.com/location/ip? ak = 0Ow2Wf7p5ypNupzzFKwy3NQCz2FFlMa9 & coor = bd09ll & callback = test' 
            document.body.appendChild (script) 
        }
     </ script>

 

Guess you like

Origin www.cnblogs.com/gzw-23/p/12725615.html