百度jsonp獲取數(shù)據(jù)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .gray{
            background: #ccc
        }
    </style>
</head>
<body>
   <div id="box">
        <input type="text" v-model="todo" @keyup="get($event)" @keydown.down="changeDown" @keydown.up.prevent="changeUp" >

    <ul>
        <li v-for="value in myData"  :class="{gray:$index==now}">
            {{value}}
        </li>
    </ul>

    <p v-show="myData.length==0">暫無數(shù)據(jù)...</p>
  </div>

    <script src="vue.js"></script>
    <script src="vue-resource.js"></script>

    <script>
        new Vue({
            el:'#box',
            data:{
                myData:[],   //存儲li里面的數(shù)據(jù)
                todo:'',
                now:"-1"
            },
            methods:{
                
                get(ev){

                //當(dāng)我按下上或者下鍵的時候,不在請求
                    if(ev.keyCode==38 || ev.keyCode==40){
                        return;
                    }

                    if(ev.keyCode==13){
                        window.open("https://www.baidu.com/s?wd="+this.todo)
                    }
                    this.$http.jsonp(
                        "https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su",
                        {
                         wd:this.todo
                        },
                        {
                            jsonp:"cb"
                         
                         }).then(function(data){
                           //吧請求到的數(shù)據(jù)添加到數(shù)組里
                            this.myData=data.data.s;
 
                    },function(err){
                        console.log("shibai")
                    })
                },

                changeDown(){

                     this.now++;
                     // 如果當(dāng)前的索引值等于數(shù)組的長度
                     if(this.now==this.myData.length){
                        this.now=-1;
                     }
                     
                     // 當(dāng)前的文本框等于數(shù)組中的當(dāng)前內(nèi)容
                     this.todo=this.myData[this.now];
                },
                changeUp(){
                    this.now--;
                    if(this.now==-2){
                        this.now=this.myData.length-1;
                    }

                       this.todo=this.myData[this.now];
                }
            }
        })
    </script>
</body>
</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容