user1660241
user1660241

Reputation: 13

What is $.ajax data: for?

I deleted var string and $.ajax data: line and still my script worked fine, so what this is for then?

  $(function() {
    $(".delete").click(function() {
    $('#load').fadeIn();
    var commentContainer = $(this).parent();
    var id = $(this).attr("id");


var string = 'id='+ id ;

$.ajax({
   type: "POST",
   url: "delete.php",
   data: string,
   cache: false,
   success: function(){
    commentContainer.slideUp('slow', function() {$(this).remove();});
    $('#load').fadeOut();
  }

 });

return false;
    });
});

Upvotes: 0

Views: 64

Answers (4)

worenga
worenga

Reputation: 5856

It contains query string parameters for the server. In this case the ID of the element that has to be deleted.

However since the server seems not to deliver another status but HTTP 200, the success handler is triggered and the element is faded out or something like that, so it seems that the element has been deleted but if you'd refresh its still there.

Upvotes: 0

Abraham
Abraham

Reputation: 20694

When a class="delete" is clicked, you're getting the id of that element and sending it to delete.php.

Upvotes: 0

Lix
Lix

Reputation: 47986

The data parameter in the ajax() function is where you place data that you want to send with the request to the server.

data (Object, String)

Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs...

In you specific case, from what I can see in the code above that you posted. Your delete.php file would receive the data send in the ajax call and you'll be able to see it in the $_POST variable in PHP.

Reference - jQuery ajax()

Upvotes: 0

xdazz
xdazz

Reputation: 160883

It is used to pass the parameter.

If in the server side, delete.php and delete.php?id={id} just do the same thing, then it's same.

But it should not be same in normal case, it's your code, under your control.

Upvotes: 1

Related Questions