zoukankan      html  css  js  c++  java
  • vue-resource: jsonp请求百度搜索的接口

    1. yarn add vue-resource

    2. main.js引入vue-resource

    import Vue from 'vue'
    import MintUI from 'mint-ui'
    import 'mint-ui/lib/style.css'
    import App from './App.vue'
    import router from './router'
    import VueResource from 'vue-resource'
    
    
    Vue.config.productionTip = false
    
    Vue.use(MintUI)
    Vue.use(VueResource)
    
    new Vue({
      router,
      render: h => h(App)
    }).$mount('#app')

    3. About.vue

    <template>
      <mt-button type="primary" size="small" @click="get()">default</mt-button>
    </template>
    
    <script>
    
    export default {
      name: 'about',
      data(){
        return{
        }
      },
      methods:{
        get(){  //引入vue-resource之后, 相当于在vue实例(this)身上新增了一些方法或对象(如:$http)
          this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su',  //jsonp参数一: 跨域请求接口;
          {
            params:{    //jsonp交互走的是get形式(不是post), 传递参数用params
              wd:'a'
            },
            jsonp:'cb'  //接口的callback名字, vue默认名字为"callback"
          },  //jsonp参数二: 传到的params和callback类型
          ).then(function(res){  //固定形式: jsonp('',{params{},jsonp:'cb'}).then(function(){'成功'},function(){'失败'});
            alert(res.data.s);
          },function(res){
            alert(res.status);
          });
        }
      }
    }
    </script>
  • 相关阅读:
    第三章 AjaxPro框架
    第一章 ASP.NET XML与JSON
    第二章 ASP.NET Ajax核心对象
    第五次作业
    第四次作业
    第三周笔记
    第二周笔记
    Java作业
    日期顺时,自动跳过节假日
    利用java实现excel转pdf文件
  • 原文地址:https://www.cnblogs.com/qq254980080/p/10345216.html
Copyright © 2011-2022 走看看