【发布时间】:2017-05-21 21:08:54
【问题描述】:
这是我在 Laravel 应用程序中的表单 html:
<button onclick="submitForm()">submit form using jquery ajax</button>
<form name="fbCommentCountform" action="{{ route('blogs.update', ['id'=>$id]) }}">
{{ csrf_field() }}
<input type="hidden" name="_method" value="PUT">
<input type="text" name="commentCount" id="fbFormCommentCount">
</form>
这是我尝试使用的 ajax Javascript 代码:
function submitForm() {
var http = new XMLHttpRequest();
http.open("POST", "{{ route('blogs.update', ['id'=>$id]) }}", true);
http.setRequestHeader("Content-type","application/x-www-form-urlencoded");
var params = "search=";
http.send(params);
http.onload = function() {
alert(http.responseText);
}
}
我对如何定义参数以使其在我的 id #fbFormCommentCount 输入框中发送数据有点困惑。我只是希望表单在按钮单击时提交,而无需重新加载页面并使用 PUT 方法,因为它用于控制器中的更新请求。任何指导将不胜感激!
【问题讨论】:
-
我也不介意使用jQuery,我只需要提交表单而不需要重新加载页面,并在id为#fbFormCommentCount的表单中发送输入字段中的一条数据。
-
把
http.open("POST"...改成http.open("PUT",...怎么样?
标签: javascript jquery ajax forms laravel