jsonp跨域

來源:互聯網
上載者:User

標籤:erer   fps   icc   ddn   ioi   zip   hdu   acm   inf   

一個簡單的本地jsonp跨域的小例子

1.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" content="content">
    <title>
        src的jsonp格式跨域
    </title>
</head>
<body>
<script type="text/javascript">
    function so(a){
        alert(a.name);
    }
</script>
<script type="text/javascript" src=‘http://www.shop.com/1.js‘></script>
</body>
</html>

1.js

so({
    name:‘niniubi‘,
    erbier:‘haishiniubi‘;
});

 

彈出 結果

 

 

 

 

<!-- 仿搜狗搜尋jsonp跨域.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>搜尋引擎</title>
</head>
<body>
    <h1>搜尋引擎</h1>
    搜尋引擎:<input type="text" name="name" onkeyup="so()">
    <ul></ul>
</body>
<script type="text/javascript">
    function so(){
        var inp = document.getElementsByTagName(‘input‘)[0];
        var url = ‘https://sug.so.360.cn/suggest?callback=suggest_so&encodein=utf-8&encodeout=utf-8&format=json&word=‘+inp.value;
        //console.log(url);
        var sc = document.createElement(‘script‘);
        sc.src = url;
        document.getElementsByTagName(‘head‘)[0].appendChild(sc);
    }
    function suggest_so(text){
        var html = ‘‘;
        for(var i=0;i<text.result.length;i++){
            html = html + ‘<li>‘+text.result[i].word + ‘</li>‘;
            //<li>hao123</li><li>hm中國官網</li>
        }
        document.getElementsByTagName(‘ul‘)[0].innerHTML = html;
    }
</script>
</html>

 


結果如下:

 

jsonp跨域

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.