Kashyap Kotak
Kashyap Kotak

Reputation: 1938

Call PHP script without expecting any return in js

I want to call a server side php script in spme special case. in normal method, we do:

    var xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function() { 
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
        //some action
    }
    xmlHttp.open("GET", theUrl, true); // true for asynchronous 
    xmlHttp.send(null);  

But herejs may be listening for readystatechange. However, in my case, the php script does not return anything. I just need to call it from js and forget it. How can that be done so that js does not wait for any response and continue other things after passing the request?

Upvotes: 1

Views: 40

Answers (1)

M. Eriksson
M. Eriksson

Reputation: 13635

There's two ways you can do this.

If you don't care at all about if the request was successful or not, just remove the onreadystatechange-listener:

var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", theUrl, true); // true for asynchronous 
xmlHttp.send(null);  

If you don't care about the response, but you want to show an error if the http-request failed:

var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() { 
    if (xmlHttp.readyState == 4 && xmlHttp.status != 200) {
        // This block will now get executed when the request is done
        // and the HTTP status is anything but 200 OK (successful)
    }
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous 
xmlHttp.send(null);  

Upvotes: 1

Related Questions