【问题标题】:Vue js making server call using vue-resourceVue js 使用 vue-resource 进行服务器调用
【发布时间】:2018-06-02 14:02:20
【问题描述】:

我正在尝试弄清楚如何使用 vue-resource 进行以下服务器调用。我不太确定如何使用 Vue.$http.post 设置标头和发送数据

jQuery.ajax({
url: "http://url/",
type: "POST",
headers: {
    "Content-Type": "application/json; charset=utf-8",
},
contentType: "application/json",
data: JSON.stringify({
    "email": "foo",
    "password": "bar
})

})

【问题讨论】:

标签: javascript vue.js vuejs2 vue-resource


【解决方案1】:

你应该可以这样做:

Vue.http.post('http://dev-api.languagelink.com/api/auth/login', {
  email: 'foo@bar.com',
  password: 'foobar',
}).then(res => {
  // Handle success response
})

vue-resource 将自动设置 Content-Type 标头并将有效负载字符串化为 JSON。

【讨论】:

    【解决方案2】:

    试试这样的:

    this.$http.post('/url', data, {
       headers: {
           'Content-Type': 'application/json; charset=utf-8'
       }
    }).then(res => {
    //do the stuff
    });
    

    【讨论】:

      猜你喜欢
      • 2016-10-24
      • 2016-10-06
      • 1970-01-01
      • 2016-01-28
      • 2015-11-11
      • 2019-10-03
      • 2017-03-26
      • 2022-11-02
      • 1970-01-01
      相关资源
      最近更新 更多