【发布时间】:2014-12-22 02:47:31
【问题描述】:
我只是在学习 jQuery 并处理 ajax 提交,但我的表单没有提交,它只是重定向到我的主页。
您在我的 AJAX 中看到错误了吗?
带有 url 和 id 的表单结构 - 在 Laravel 中使用刀片模板:
{{ Form::open(
array(
'url' => '',
'class' =>'form-horizontal',
'id' => 'invoiceForm',
'method' => 'post'
)
) }}
ajax 调用(注意:我使用的是 jQuery 验证插件)
$(document).ready(function(){
$('#invoiceForm').validate({
rules:{
title: {
required: true,
maxlength: 255,
}
},
messages:{
title: {
required: 'Please enter a title.'
}
},
submitHandler: function(form) {
var title = $('#title').val();
var customerId = $('#customer').val();
var contractorId = $('#contractorId').val();
var taxRate = $('#taxRate').val();
var servicename = $('#taxRate').val();
var taxable = $('#taxable').val();
var qty = $('#qty').val();
var price = $('#price').val();
var subtotal = $('#subtotal').val();
var total = $('#gtotal').val();
var comments = $('#comments').val();
$.post(baseURL+'/contractors/invoice',
{
title: title,
customerId: customerId,
contractorId: contractorId,
taxRate: taxRate,
serviceName: serviceName,
taxable: taxable,
qty: qty,
price: price,
subtotal: subtotal,
total: total,
comments: comments
},
function(response){
if(response.status == 200) {
location.href=baseURL+'/contractors/dashboard';
}
else if(response.status == 400) {
$('#msgSection').empty().removeClass('alert-error alert-success').addClass('alert-error');
}
}, 'json');
return false;
}
});
验证有效,但我认为表单没有捕获 .post 操作。 Firebug 不显示任何错误,只是重定向到索引页面。
这是我的发布路线(提交到表单所在的同一页面):
Route::post('/contractors/invoice', 'ContractorController@postInvoice');
另外,可能需要注意的是,表单可以与 php 提交操作一起正常工作。
TIA
【问题讨论】: