user1022585
user1022585

Reputation: 13641

jquery ajax post querystring

Just getting to grips with this jquery and ajax thing.

I want to run a small script on a page and I've gathered I need to use POST from a jquery function to do this. I'm having trouble sending querystrings along though, what am I doing wrong?

$.post("inventory.php?use=" + drag_item.attr('id'));

the drag_item.attr('id') is a small one word text, is that the right way of doing it?

Upvotes: 1

Views: 4805

Answers (2)

Dylan Vorster
Dylan Vorster

Reputation: 56

$.post("inventory.php?use=" + drag_item.attr('id')); //wrong

this is wrong, it takes an additional set of params for this purpose:

$.post("inventory.php",{use:''+drag_item.attr('id')},function(responseData){
    //callback goes here
});

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You should encode the parameters:

$.post("inventory.php", { use: drag_item.attr('id') });

Also in this example you are only sending an AJAX request but never subscribing to any success callback in order to process the results returned by the server. You could do this like that:

$.post("inventory.php", { use: drag_item.attr('id') }, function(result) {
    // this will be executed when the AJAX call succeeds and the result
    // variable will contain the response from the inventory.php script execution
});

Also make sure that the drag_item that you are using in this example has been properly initialized to some existing DOM element and that this DOM element has an id attribute.

Finally use a javascript debugging tool such as FireBug in FireFox or Chrome developer toolbar in Google Chrome to debug your AJAX requests and see the requests and responses being sent to and from the server and any possible errors that might occur in between.

Upvotes: 1

Related Questions