11, launched in ajax request in Vue

Vue-resource achieve get, post, jsonp request

Vue and Vue-resource is highly integrated third-party packages

There is also a request sent outside ajax Another axios third party package implements the requested data.

The first step, download Vue-reource

A second step of introducing at Vue-resource Vue

The third step is to create an access request

get

Use button trigger event,

getInfo() {//发起get请求
    this.$http.get('http://vue.studyit.io/api/getlunbo').then(function (result) {
        console.log(result.body)
    })
}

Wherein the get request return result is the result set, the result set can be acquired by the output

However, this seemingly unavailable address, get the result by the data returned by the server

post

postInfo() {//发起get请求
    // this.$http.post('http://vue.studyit.io/api/post',{},{}).then(function (result) {
    //     console.log(result.body)
    // })
    this.$http.post('http://vue.studyit.io/api/post',{},{}).then(result=> {
        console.log(result.body)
    })
}
//发起post请求,application/x-www-form-unlencoded
//手动发起的post请求是没有表单格式的,所以,有些服务器处理不了
//所以需要在第三个参数里面传入emulateJSON:true,将提交的数据类型转换成普通表单的数据格式

Wherein {} is the first incoming data to the server

jsonp

jsonpInfo(){
    this.$http.jsonp('http://vue.studyit.io/api/post').then(result=>{
        console.log(result.body)
    })
}

jsonp request must be set up virtual server request mode, Continued "" "" "

Guess you like

Origin blog.csdn.net/weixin_34247155/article/details/91026134