Robert Benedetto
Robert Benedetto

Reputation: 1710

$.get() does not work?

UPDATED!!

The code after

   $.get(url, { id: id, action: action }, function(data) {

does not fire? Complete method below.

   function modifyPermit(id, action) {
    var url = "ajax/permit_modify.aspx";
    $.get(url, { id: id, action: action }, function(data) {
        if (data == "SUCCESS") {
            // Update page
            $.get("ajax/permit_getall.aspx", function (data) {
                $("#ctl00_ContentPlaceHolder1_permitList").html(data);
            }).error(function(err) {
               alert(err);
            });

        } else {
            alert(data);
        } 
    });
}

Using .Net.

Regards,

Robert

Upvotes: 0

Views: 90

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

You seem to be using a wrong variable for the url here (you defined url_mod but used url in your AJAX request):

var url_mod = "ajax/permit_modify.aspx?id=" + id + "&action=" + action;
$.get(url, function(data) {

which should should become:

var url = "ajax/permit_modify.aspx?id=" + id + "&action=" + action;
$.get(url, function(data) {

Also instead of manually having to call the encodeURIComponent function for each parameter (which by the way you forgot to do in your second AJAX request) I would recommend you using the following construct:

var url = 'ajax/permit_modify.aspx';
$.get(url, { id: id, action: action }, function(data) {

This way jQuery will take care of generating the properly encoded url. The same concept could be applied in your first AJAX request. Notice how you could pass as second parameter to the $.get function key/value pairs that jQuery will use to construct the proper url.


UPDATE:

After having an offline chat with the OP it turned out that the problem was not related to the AJAX code shown in his question but rather with the way he was calling the modifyPermit function. It was inside the onclick handler of a button without returning false. This in turn triggered a full postback to the server leaving no time for the AJAX request to execute. The correct solution was to return false to prevent the default action of the button:

onclick="modifyPermit(1,'a'); return false;"

Upvotes: 2

Robert Benedetto
Robert Benedetto

Reputation: 1710

Fix found by @darin which tuned out to be simple: just add return false; after the method call on the button.

modifyPermit(1,'a'); return false;

Many thanks to Darin for all the time and effort he put in the helping me solve this issue. Could not have don it without you!

/Bob

Upvotes: 0

Related Questions