1. What is JSONP callback({"name": "王欢"}); SONP consists of two parts : callback function and data. The callback function is the function that should be called in the page when the response comes. The name of the callback function is usually specified in the request. The data is the JSON data passed into the callback function. The following is a typical JSONP request. https://freegeoip.net/json/?callback=handleResponse This URL is requesting a 2. JSONP cross-domain request We know that the same-origin policy is a security mechanism of the browser. The so-called source refers to the protocol, domain name and port number. When our script is running, the browser will detect whether the script it executes and the data it obtains are the same as our HTML page. If they are the same, they are of the same origin and a successful request will be made. If their sources are different, it is a cross-domain request. By default, browsers do not support cross-domain requests. So what should we do if we want to make a cross-domain request? <script> function getData(data){ console.log(data); } var script = document.createElement('script'); script.id = 'jsonp'; script.src = 'jsonp.js'; document.body.appendChild(script); </script> Assuming that the front-end has told the back-end the function name, the back-end can call getData({ name: 'Xiao Wang', age: 20 }) The result of running is: We get an Type "b" in the search box, and the request will be as shown below: The requested keywords are: The https://www.baidu.com/sugrec?pre=1&wd=b&req=2&csor=1&cb=getData(); Test it by typing this into the address bar: It can be found that this callback function becomes the one we set. 3. Simulate Baidu search We can now use this interface to generate JSON to simulate the Baidu search page. The code is as follows: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> div { position: relative; width: 600px; height: 40px; } input { width: 500px; height: 40px; border: 2px solid #4E6EF2; } button{ position: absolute; left: 411px; top: 0; width: 95px; height: 44px; background-color: #4E6EF2; border: none; font-size: 18px; color: white; } ul{ position: relative; left: -40px; top: -10px; width: 411px; height: 400px; } li{ height: 40px; width: 411px; line-height: 40px; font-size: 16px; list-style: none; } </style> </head> <body> <div> <input type="text" value =''> <button>Search on Baidu</button> </div> <ul></ul> <script src="jquery.js"></script> <script> // function getData(data){ var script = document.querySelector('#jsonp'); script.parentNode.removeChild(script); $('ul').html(''); for(var i =0;i<data.g.length;i++){ $('<li>'+data.g[i].q +'</li>').appendTo('ul'); } } //Dynamically generate script function getList(wd){ var script = document.createElement('script'); script.id = 'jsonp'; script.src = 'https://www.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=pc&from=pc_web&sugsid=26350&req=2&csor=1&cb=getData&wd='+wd; document.body.appendChild(script); } //var ipt = document.querySelector('input'); ipt.addEventListener('keyup',function(){ var wd = this.value; getList(wd); console.log(wd); }) </script> </body> </html> The effect is: JSONP DisadvantagesJSONP is extremely popular among developers because it is very simple and easy to use, but it also has two disadvantages:
This is the end of this article about JSONP cross-domain simulation Baidu search. For more relevant JSONP cross-domain simulation Baidu search content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Share 8 very useful CSS development tools
>>: Introduction to Docker Quick Deployment of SpringBoot Project
1. Use the <nobr> tag to achieve no line bre...
environment Hostname IP address Serve Prometheus ...
Preface I recently wanted to learn CocosCreator, ...
This article shares the specific code for JavaScr...
Table of contents sequence 1. Centralized routing...
1. Flex is the abbreviation of Flexible Box, whic...
Table of contents 1. Import files using script ta...
This article shares the specific code of vue3 enc...
Method 1: Install the plugin via npm 1. Install n...
This article example shares the specific code of ...
Table of contents 1. Database design 2. Front-end...
Find the problem I have been learning Django rece...
Table of contents background Achieve a similar ef...
The pitfalls 1. Many tutorials on the Internet wr...
<br />Conditional comments are a feature uni...