Javascript 在 VueJS 中使用 POST 请求提交表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51421277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Submit a form using POST request in VueJS
提问by Dinesh Ahuja
I have an endpoint to submit data using POST request,
我有一个端点可以使用 POST 请求提交数据,
http://localhost:3000/entry
Keys are fname, lname, age
键是 fname, lname, age
I can make a POST request into the given end point and it will create an entry.
我可以向给定的端点发出 POST 请求,它会创建一个条目。
I am trying to submit a form using VueJS. But, when I am calling the API within the form, it is not submitting the data. I have checked the network calls and it is not sending any data to the end point.
我正在尝试使用 VueJS 提交表单。但是,当我在表单中调用 API 时,它没有提交数据。我检查了网络调用,它没有向端点发送任何数据。
HTML :-
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 :-
脚本.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 :-
style.css :-
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
回答by Ziaur Rahman
You can try this one.
你可以试试这个。
axios.post('http://localhost:3000/entry',{ params: { fname: this.fname, lname: this.lname, age: this.age }})
.then(response => this.responseData = response.data)
.catch(error => {});
Before using you need to link with Axios CDN
使用前需要链接Axios CDN
https://cdn.jsdelivr.net/npm/[email protected]/dist/axios.min.js
https://cdn.jsdelivr.net/npm/[email protected]/dist/axios.min.js
回答by ribas
Maybe something like this
也许像这样
this.$http({method: 'POST', url: URL, data: data})
.then(response => {
this.postResults = data;
this.ajaxRequest = false;
}).catch((err) => {
console.log(err)
})

