我有一个端点可以使用POST请求提交数据,
http://localhost:3000/entry
键为fname, lname, age
我可以向给定的端点发出POST请求,它将创建一个条目。
我正在尝试使用VueJS提交表单。但是,当我在表单中调用API时,它没有提交数据。我已经检查了网络呼叫,并且没有将任何数据发送到终点。
HTML:-
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.4/vue.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-resource/0.1.16/vue-resource.min.js"></script>
<script src="script.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="style.css">
<div id="vueApp">
<div class="container">
<div class="row">
<div class="col-sm-12">
<h3>
Dashboard
</h3>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<label for="fname">First Name</label>
<input type="text" class="form-control" value="" v-model="fname" />
</div>
<div class="form-group">
<label for="lname">Last Name</label>
<input type="text" class="form-control" value="" v-model="lname" />
</div>
<div class="form-group">
<label for="age">Age</label>
<input type="text" class="form-control" value="" v-model="age" />
</div>
</div>
<div class="col-sm-12">
<a href="#" class="btn btn-success" @click="submitEntry">Submit</a>
<span v-if="ajaxRequest">Please Wait ...</span>
</div>
</div>
<div> </div>
<div class="row" v-if="debug">
<div class="col-sm-12">
<pre>{{ $data | json }}</pre>
</div>
</div>
<!-- Table Start -->
<div class="row">
<table style="width:100%">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
<tr>
<td>{{fname}}</td>
<td>{{lname}}</td>
<td>{{age}}</td>
</tr>
</table>
</div>
<!-- Table END -->
</div>
</div>
script.js:-
Vue.http.options.emulateJSON = true;
new Vue({
el: '#vueApp',
data: {
debug: true,
fname: '',
lname: '',
age: '',
ajaxRequest: false,
postResults: []
},
methods: {
submitEntry: function() {
this.ajaxRequest = true;
this.$http.post('http://localhost:3000/entry', {
fname: this.fname,
lname: this.lname,
age: this.age
}, function (data, status, request) {
this.postResults = data;
this.ajaxRequest = false;
});
}}
});
style.css:-
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
答案 0 :(得分:1)
即使有点老,我也遇到了同样的问题,没有数据发送到端点(这个问题首先出现在Google中)。问题在于vue-resource的$ http.post()默认将数据发布为resource / json和 not 形式。要发布 form ,请设置emulateJSON to true which will send the "request body as application/x-www-form-urlencoded content type"。
顺便说一句,结果是一个Promise,这意味着then()应该用于处理响应(如@ribas正确提到的那样)。
因此,上例中的submitEntry()应该为:
submitEntry: function() {
this.ajaxRequest = true;
this.$http.post('http://localhost:3000/entry', {
fname: this.fname,
lname: this.lname,
age: this.age
}, {
emulateJSON: true // <-- This was missing
}).then(function (data) { // <-- Handle results like this
this.postResults = data;
this.ajaxRequest = false;
});
}}
使用完全不同的库(axios)很好,实际上是recommended by Evan You (creator of Vue),但不能使用vue-resource ajax库解决实际问题。
答案 1 :(得分:0)
也许是这样
this.$http({method: 'POST', url: URL, data: data})
.then(response => {
this.postResults = data;
this.ajaxRequest = false;
}).catch((err) => {
console.log(err)
})
答案 2 :(得分:0)
您可以尝试这个。
axios.post('http://localhost:3000/entry',{ params: { fname: this.fname, lname: this.lname, age: this.age }})
.then(response => this.responseData = response.data)
.catch(error => {});
在使用之前,您需要与axios CDN链接
https://cdn.jsdelivr.net/npm/axios@0.12.0/dist/axios.min.js