开发者

Simple problem on jQuery ajax method

开发者 https://www.devze.com 2023-04-05 17:59 出处:网络
I am trying to simply use jQuery\'s ajax method and method only call error callback. $(\'form\').submit(function() {

I am trying to simply use jQuery's ajax method and method only call error callback.

$('form').submit(function() {
    $.ajax({
        url: this.action,
        data: $(this).serialize(),
        type: 'POST',
        dataType: 'html',
        success: function() {
            debugger
            alert('alert');
        },
        error: function(xhr, status, error) {
            //status value is "error".
        }
    });
});

I am requesting to ASP.NET MVC action method. Method get request as expected and return partial view without any problem. Then jquery call error callback without specify detailed error info. I want to know some details about error then i can decide what can cause it.

Edit : I have tested below code and it works without problem. I just canceled form submit event.

$('form').submit(function(e) {
    e.preventDefault();
    $.ajax({
        url: this.action,
        data: $(this).serialize(),
        type: 'POST',
        dataType: 'ht开发者_开发技巧ml',
        success: function() {
            debugger
            alert('alert');
        },
        error: function(xhr, status, error) {
            //status value is "error".
        }
    });
});

Still answer is expecting right answer who can tell me reason of this error.


Could it be the reference this.action is undefined? Look at the network tab of your favorite dom inspector or just the error console of firebug. Does the POST request look as expected? (Check the URL, data sent, error code, etc)

$(this).attr('action') will certainly work, assuming this points to the form.

0

精彩评论

暂无评论...
验证码 换一张
取 消