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) {

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.

    cache: false,
    type: "GET",
    url: this.action,
    success: function (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


Leave a Comment

WP to LinkedIn Auto Publish Powered By :