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. View openjdk rpm -qa|grep jdk 2. Delete openjd...
Configuring Alibaba Cloud Docker Container Servic...
1. Command Introduction The date command is used ...
This article shares the specific code of JS to ac...
Problem Description There is a type of query call...
This article is used to record the installation o...
Preface: When we need to store decimals and have ...
npx usage tutorial Tonight, when I was learning V...
A few days ago, when I was adjusting a module of a...
Table of contents 1 Java environment configuratio...
1. Prerequisites We use the require.context metho...
1. IE browser mode Hack logo 1. CSS hack logo Copy...
LocalStorage stores Boolean values Today, when I ...
Preface Some of the earlier codes on Github may r...
First, download the installation package from the...