Using jsonp to realize Baidu association words

The so-called json is a "usage mode" of json, which can be used to solve the problem of cross domain data access of mainstream browsers. Th...

The so-called json is a "usage mode" of json, which can be used to solve the problem of cross domain data access of mainstream browsers. The next case to be written is to use this principle.

First of all, we can input some content in Baidu search

Next, open the Network of the console, and you can see the above figure. Next, copy the content of the Request URL. The content indicated by the arrow is actually the search function, that is, cb = function name. Next, look at the following code

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> * .box .list </style> </head> <body> <input type="text"></div> <div></div> <script> var obox = document.getElementsByClassName("box")[0], list = document.getElementsByClassName("list")[0]; obox.oninput = function () { var val = this.value;//Get input box content var os = document.createElement("script") os.src = `https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=$&cb=getdata` document.body.appendChild(os); os.onload = function () { document.body.removeChild(os) } } var getdata = function (data) { var str = ''; for (let i = 0; i < data.s.length; i++) { str += `<li>$</li>` } list.innerHTML = str } </script>

Because the function given by the console is too long, I changed it to getdata function, and the function after wd = is actually the search content, so the association word search is realized

3 December 2019, 22:55 | Views: 2697

Add new comment

For adding a comment, please log in
or create account

0 comments