1

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.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Steve
  • 93
  • 2
  • 9
  • You could try that : http://stackoverflow.com/questions/4301968/checking-a-url-in-jquery-javascript But you could have a cross-domain request problem – sdespont Jan 25 '13 at 20:44
  • If you're going cross-domain, you can't do anything but `GET`. In the same domain, just set `type` to `HEAD` and you're good to go – Explosion Pills Jan 26 '13 at 00:31
  • @ExplosionPills: Are you saying that even with [tag:CORS] you can still only use `GET`? – hippietrail May 21 '13 at 01:12
  • @hippietrail no, with cors you can make any kind of HTTP request; I was referring to jsonp (this was a while ago!) – Explosion Pills May 21 '13 at 01:16
  • @ExplosionPills: Ah OK it seems like the OP didn't bother putting any such relevant details in the question (-; – hippietrail May 21 '13 at 01:34

1 Answers1

2

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();           
}
});
StratusBase LLC
  • 289
  • 1
  • 5
  • 16