Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

This is a typical Jquery ajax request.

$.get(this.action, $(this).serialize(), function (data) {
    $('#expungeResult').html(data);
})

This works great until you realize that it is really taking data from a form and is changing data on the site. It should be a post, but it was mistakenly made with a get. The controller method was defined as POST only. This causes the an error in the server log.

Request method ‘GET’ not supported

Jquery $.get and $.post do support chaining .success(), .error() and .complete() methods. Unfortunately, they don’t have parameters.

$.ajax has what you need, at the expense of a more verbose declaration.

$.ajax({
    cache: false,
    type: "GET",
    url: this.action,
    data:$(this).serialize(),
    success: function (data) {
        $('#expungeResult').html(data)
    },
    error: function (jqXHR, textStatus, errorThrown) {
        $('#expungeResult').html(textStatus).append("...").append(errorThrown).css('color', 'red')
    }}
);

Now, “error…Request method ‘GET’ not supported” is displayed in red in my display area. An ugly message, but at least the user will not keep clicking the button because it looks like nothing happened.

June 12th, 2012

Posted In: javascript, JQuery

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *

WP to LinkedIn Auto Publish Powered By : XYZScripts.com