VueJs如何发送带参数的帖子请求?

时间:2017-06-11 12:14:11

标签: javascript post vuejs2

我以这种方式发送请求

this.$http.post('http://localhost:3000/store', {something: 'string'}, {
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
      }})

如果您查看浏览器,参数看起来像这样

enter image description here

在图像中我显示的对象看起来像({“something”:“string”}:)

请注意,我的对象是另一个对象的键 (如果使用json.stringfy(),我的对象仍然是另一个对象的键) 为什么会发生以及如何制作普通物体?

2 个答案:

答案 0 :(得分:0)

我认为最好的方法是首先创建对象。



var something = "key";
var obj = {};
obj[something] = 'string';

this.$http.post('http://localhost:3000/store', obj, {
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
      }})




答案 1 :(得分:-1)

<script>

var mixin = {


        var url = 'http://localhost/vue/serv.php';
        this.$http.get(url, {params: {action: 'buscar', userid:'2'}, headers: {'X-Custom': '...'}}).then(response => {
            this.usuarios = response.body;
            console.log(response);
        }, response => {
            console.log(error);
        });
}    
    }


new Vue({
    el: '#resultado', 

    mixins: [mixin], 

    data:{
        titulo: "Vue lmideias",       
        usuarios: []        
    } 

});

</script>
相关问题