I'm using jQuery $.ajax to add elements on-the-fly to my database.
The server-side returns the ID of the element recently added, and I want to capture that in my done event block.
if(!key) {
$.ajax({
url: "/tags/add",
type: "GET",
data: {tagName:value}
}).done(function(data){
key = data;
});
}
/tags/add returns:
return new Response($tags[0]->getId(), 200, array('Content-Type' => 'text/html'));
where Response is framework specific.
If I go to the URL /tags/add?tagName=123
it will attempt to add that tag to the database if it doesn't exist. If it is succesful, it will return its id.
In the example above, in returns 63
but the done function doesn't seem to be catching that 63
(or whatever the number is)
Here's the catch.
Let's assume the tag dogs
doesn't exist:
1) /tags/add?tagName=dogs
will add dogs
to the database, and will return ID 64
2) If I do console.log(data)
in the first line inside the done event block, it will log 0
(default ID when the tag doesn't exist)
3) If I try to add dogs
tag again to the database, /tags/add?tagName=dogs
will see that dogs
already exists, and will only return its id (64
)
4) Then console.log(data)
kicks in again, and it logs 64
Any idea why is that happening?