十.vue-resource实现get, post, jsonp请求
本文最后更新于 1537 天前,其中的信息可能已经有所发展或是发生改变。

vue-resource 实现 get, post, jsonp请求

methods:{
    getInfo(){
        //  当发起get请求之后, 通过 .then 来设置成功的回调函数
        this.$http.get('http://vue.studyit.io/api/getlunbo')
        .then(result=>{
            console.log(result)
        })
    },
    postInfo() { 
        // 发起 post 请求   application/x-www-form-urlencoded
        //  手动发起的 Post 请求,默认没有表单格式,所以,有的服务器处理不了
        //  通过 post 方法的第三个参数, { emulateJSON: true } 设置 提交的内容类型 为 普通表单数据格式
        this.$http.post('http://vue.studyit.io/api/post', {}, { emulateJSON: true }).then(result => {
            console.log(result.body)
        })
    },
    jsonpInfo() { 
        // 发起JSONP 请求
        this.$http.jsonp('http://vue.studyit.io/api/jsonp').then(result => {
            console.log(result.body)
        })
    }
}

参考文档
* 如果我们通过全局配置了,请求的数据接口 根域名,则 ,在每次单独发起 http 请求的时候,请求的 url 路径,应该以相对路径开头,前面不能带 / ,否则 不会启用根路径做拼接;

Vue.http.options.root = 'http://vue.studyit.io/';
  • 全局启用emulateJSON选项
Vue.http.options.emulateJSON = true;
  • 初始化函数应该在created钩子函数里
created() { // 当 vm 实例 的 data 和 methods 初始化完毕后,vm实例会自动执行created 这个生命周期函数
  this.getAllList()
},
作者:Yuyy
博客:https://yuyy.info
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇