Jump to content

JQuery $.ajax() Basics


w3schoon

Recommended Posts

Nice be with you everyone! I'm planing to use JQuery $.ajax() method to delete 1 row of my database. While searching I found this on the web...

 			$.ajax({				type: 'get',				url: '/examples/delete.php',				data: 'ajax=1&delete=' + $(this).attr('id'),				success: function() {					parent.fadeOut(300,function() {						parent.remove();				});			}		});

I have questions: 1. What is the difference of post & get in the type: ?2. What is the usage of 'ajax=1&delete=' in the data: ?3. If there is success: how about if not success, I mean error handler? Thank you.

Link to comment
Share on other sites

1 + 3) you can get from the documentationhttp://api.jquery.com/jQuery.ajax/ but basically it specifies the type of request you are making to the server, which will in turn determine how you implement it. (i.e. GET vs. POST). It has more meaning in the context of REST'ful HTTP requests, but that might be beyond the scope of this example. 2) it is a query string. ajax=1 could indicate that the request is being initiated from the client side via the web application itself (not a direct access of delete.php, although this could be spoofed), and delete could confirm the kind of request that is being made. This all specific to the internals of delete.php itself however.

Link to comment
Share on other sites

Thank you very much, now I understand. How about ajax=2 or 3 or 4 or n... ?
2) it is a query string. ajax=1 could indicate that the request is being initiated from the client side via the web application itself (not a direct access of delete.php, although this could be spoofed), and delete could confirm the kind of request that is being made. This all specific to the internals of delete.php itself however.
the point I was trying to make is that only the developer knows. As JSG said, you could make it anything you want.
Link to comment
Share on other sites

JSG is justsomeguy. No one likes typing all that. Some people call me DD. Same reason. There is no special meaning to ajax=1 or any other query string. It is not part of the HTML rules or the jQuery API. The word "ajax" in a query string is an example of a variable name chosen by the developer. Instead of "ajax=1" the developer could have written "w3schoon=1". For the same reason, there is no special meaning to "1" or "2" or "3" . . . It's just an example. YOU are the developer. You choose the data you want to send to your server in a query string. You also program a script on your server to understand what that data means.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...