Steve
Steve

Reputation: 103

jQuery ajax HEAD request

I'm writing a script for work and I need to know if this will work:

I am on one site that I need to be at in order to get the correct data, and can do this fine with a number of ajax requests. What I also need to do in the same action is request the URL on our site to see if it exists (404, 503, 200?) or not. I do not need the data on the page. I know about same origin policy but I'm not sure if I can send a HEAD request and only get the status code reliably.

Upvotes: 1

Views: 7046

Answers (1)

StratusBase LLC
StratusBase LLC

Reputation: 285

Using jQuery you could do the following, obviously replacing 'yourdomain.com' and 'someImage.jpg' with the target location/file :

$.ajax({
url : "https://yourdomain.com/someImage.jpg",
type : 'HEAD',
success : function(){
   //file exists
   doSomething();           
},
error : function(){
   //file not exists
   doSomethingElse();           
}
});

Upvotes: 2

Related Questions