我正在尝试通过Laravel 4 App中的Ajax提交表单。
问题:我刚被重定向到路线。因此,不会发送任何Ajax调用。该表格就像普通表格一样工作。
表格:
{{ Form::open(array('route' => 'ajax.profile.edit', 'method' => 'post', 'id' => 'edit-profile' )) }}
在我看来,JS代码:
$(document).ready(function($){
$( '#edit-profile' ).on( 'submit', function() {
//Form is located in a Modal, so first hide it
$("#form-primary").niftyModal('hide');
//Show a modal with a "loading" note
$('#processing-modal').modal('show');
$.post(
$( this ).prop( 'action' ),
{
"_token": $( this ).find( 'input[name=_token]' ).val(),
"displayname": $( '#displayname' ).val(),
"bio": $( '#bio' ).val(),
"expertise": $( '#expertise' ).val(),
"location": $( '#location' ).val(),
},
function( data ) {
//now hide the "loading" note and alter the success message.
$('#processing-modal').modal('hide');
alert(data.msg);
},
'json'
);
return false;
} );
} );
这应该可行,我以完全相同的方式使用它(通过AJAX提交Laravel表单)。
$('#edit-profile').submit(function (event) {
var form = $(this);
$.ajax({
type: form.attr('method'),
url: form.attr('action'),
data: form.serialize()
}).done(function (data) {
// on success
}).fail(function (data) {
// on error
});
event.preventDefault();
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句