PDA

View Full Version : jQuery ready states



phantom007
07-02-2008, 02:06 PM
Hi,

Just wondering whats the function used in jQuery for ajax XMLHttpRequest

Like the following


0 The request is not initialized
1 The request has been set up
2 The request has been sent
3 The request is in process
4 The request is complete


More info here: http://www.w3schools.com/ajax/ajax_xmlhttprequest.asp



Thanx

WA
07-02-2008, 11:53 PM
You shouldn't ever need to probe those individual states when using jQuery to make an Ajax connection. The function jQuery.ajax() supports the "error", "success", and "complete" parameters that let you fire your own custom code for any of the relevant Ajax outcomes. Something like:


$.ajax({
url: "myscript.php"
async: true,
error:function(ajaxrequest){
alert('Error fetching content.<br />Server Response: '+ajaxrequest.responseText)
},
success:function(content){
alert(content)
}
})

Native XMLHttpRequest states like 1 or 2 don't ever fire at all in some browsers, so they are not reliable anyway.

phantom007
07-03-2008, 05:59 AM
Hi,

Can this be done with $.get method?

If yes, can ya show an example plz?


Thanx

WA
07-03-2008, 08:54 AM
Well, $.get() is basically just a shorthand, limited version of $.ajax(). I don't believe it gives you access to some of the states you mentioned:

Ajax/jQuery.get: (http://docs.jquery.com/Ajax/jQuery.get#urldatacallbacktype)


Load a remote page using an HTTP GET request.
This is an easy way to send a simple GET request to a server without having to use the more complex $.ajax function. It allows a single callback function to be specified that will be executed when the request is complete (and only if the response has a successful response code). If you need to have both error and success callbacks, you may want to use $.ajax.

Why not just use the lower level, $.ajax() method?

phantom007
07-03-2008, 09:51 AM
Question:

Can I have 2 Submit buttons in a form one for adding and the other for deleting?

Well, I wanna do the addition and deletion in jQuery.

How do I implement the condition statement in jQuery that if submit button is add then do the add function and if the submit button is delete then do the delete function?


Thanx

A1ien51
07-07-2008, 02:36 PM
Sounds like you need two different functions that do either of those steps.

Eric