Home  >  Article  >  Web Front-end  >  Implement ajax to obtain cross-domain data (graphic tutorial)

Implement ajax to obtain cross-domain data (graphic tutorial)

亚连
亚连Original
2018-05-21 15:52:251775browse

This article mainly introduces in detail how to simply implement ajax to obtain cross-domain data. It has a certain reference value. Interested friends can refer to it.

The examples in this article are shared with you. The specific code for ajax to obtain cross-domain data is for your reference. The specific content is as follows

1. Rendering

##2. Source code

<html>
<head>
  <script type="text/javascript" src="/js/jquery.min.js"></script>
  <script type="text/javascript">
    function getData(keyword) {
      var url="https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=" +keyword;
      $.ajax({
        type: "get",
        async: false,
        url: encodeURI(url),
        dataType: "jsonp",
        jsonp: "cb", // 后台定义的回调函数标识符(一般默认为:callback)
        jsonpCallback: "jsonCallback", // 自定义的jsonp回调函数名称(默认为jQuery自动生成的随机函数名)
        success: function(data,status){
          console.log(data);
          console.log(status);
        },
        error: function(){
          alert(&#39;fail&#39;);
        }
      });
    }
  </script>
</head>
<body>
  <button onclick="getData(&#39;我&#39;);">百度搜索"我"</button>
</body>
</html>

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

thinkPHP ajaxMethod to achieve statistical page pv views

Ajax The concepts and advantages

Three implementation methods of

ajax

The above is the detailed content of Implement ajax to obtain cross-domain data (graphic tutorial). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn