2

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: 'html',
        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.

6
  • I have checked this.action and it return form's absolute url.
    – AnyOne
    Sep 17, 2011 at 10:31
  • You say "Method get request as expected"; do you mean the controller is expecting a GET request? Because you're POST'ing the request. Also, you'll want to return false; the form submit after the AJAX call. Also, does Firebug give you any errors in the console?
    – Alex
    Sep 17, 2011 at 10:49
  • @Alex - NO, i mean method just getting request. Controller method works without problem. And chrome debug console doesnt throw any error. But why i am getting error if i dont call e.preventDefault()
    – AnyOne
    Sep 17, 2011 at 10:54
  • I forget to paste new code. Now question is edited again.
    – AnyOne
    Sep 17, 2011 at 10:58
  • Is the request cross-domain? I.e. from one domain name to another (even localhost to something else)?
    – Alex
    Sep 17, 2011 at 11:00

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.