The principle of jsonp: It is to use the tag without cross-domain "vulnerabilities" to achieve the purpose of communicating with a third party. When communication is needed, the script of this site creates a script tag, address Point to the third-party API</p> <p> address and provide a callback function to receive data. The third-party generates a corresponding package for json data, so the browser will call the callback() function and pass the parsed json object as a parameter. . </p> <p>The core of jsonp:</p> <p> Dynamically create and add script tags to call the js script provided by the server. </p> <p> Not much else to say, just go to the code: </p> <p> HTML code; </p> <div class="cnblogs_code"><pre><body>2 <input type="text" name="text" id="text" value="" />3 <div id="div"></div>4 </body></pre></div> <p> JS code: </p> <div class="cnblogs_code"><pre> <script> 2 //封装一个jsonp方法 3 function jsonp(json){ 4 //判断路径是否正确 5 if(!json.url){ 6 alert("请输入正确路径"); 7 return; 8 } 9 //设置默认值10 json.data = json.data || {};11 json.cbName = json.cbName || 'cb'; var fnName = "show" + Math.random();15 fnName = fnName.replace(".","");16 window[fnName] = function(json2){17 json.success && json.success (json2);18 oHeade.removeChild(oScript);19 }20 json.data[json.cbName] = fnName;21 var arr = [];22 for(name in json.data){23 arr.push(name + '=' + json.data[name]);24 } //创建script标签27 var oScript = document.createElement("script");28 //设置script的src属性29 oScript.src = json.url + '?' + arr.join("&");30 //获取head标签31 var oHeade = document.getElementsByTagName("head")[0];32 //将动态创建的script标签添加到head中33 oHeade.appendChild(oScript);34 } window.onload = function(){38 var oText = document.getElementById("text");39 oText.onkeyup = function(){40 jsonp({41 url:'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su',42 data:{43 wd:oText.value44 },45 success:function(json){46 var oDiv = document.getElementById("div").innerHTML = json.s;47 } } This encapsulates a jsonp method. . .