使用javascript和jquery将表单发布到API [英] Post form to an API with javascript and jquery
本文介绍了使用javascript和jquery将表单发布到API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对表格不好.如何使用JavaScript将发布表单从underscore.js模板发送到express.js API?我尝试使用.submit(),但是它不起作用.我使用的是jQueryMObile,因此该表单位于数据角色页面内.
Im not good with forms. How do you send a post form from an underscore.js template to an express.js API with javascript? I try with the .submit() but it doesnt work. Im using jQueryMObile so the form is inside a data-role page.
服务器上的路由:
app.put('/users/:userId/spot/:spotId/add', spot.addUserSpot);
表格:
<form id="follow" method="put">
<input type="hidden" id="spot" value="<%= this.model._id %>">
<input type="hidden" id="user" value="<%= user %>">
<input type="submit" id="submitButton" value="Follow spot">
</form>
jQuery("#follow").on('submit', function(){
console.log("SUBMITTED");
var spot = jQuery("#user").attr("value");
var user = jQuery("#spot").attr("value");
jQuery("#follow").attr("action", "/users/"+user+"/spot"+spot+"/add");
alert(jQuery("#follow").attr("http://127.0.0.1:3000/users/"+user+"/spot/"+spot+"/add"));
});%>
推荐答案
大多数浏览器不支持PUT,但是express.js具有内置的解决方案.只需在您的表单中添加方法替代:
Most browsers do not support PUT, but express.js has a built-in solution. Just add a method override to your form:
<form id="follow" method="post">
<input type="hidden" name="_method" value="put" />
这篇关于使用javascript和jquery将表单发布到API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文