2

I use the simple AJAX and use google debug then find that the url is not exist...

The code is very simple:

var http;

if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
  http=new XMLHttpRequest();
} else {
  http=new ActiveXObject("Microsoft.XMLHTTP");
}

try {
  http.open("GET", 'http://'+ip+':5000/test.html', true);
  http.onreadystatechange = onRcvData;
  if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    http.send(null);
  } else {// code for IE6, IE5
    http.send();
  }
} catch(e) {
  http.abort();
}

function onRcvData() {
  if (http.readyState==4) {
    if (http.status==404) {

    } else if(http.status==200) {

    } else {

    }
  }
}

It's okay if the file test.html exists. When the file isn't exist, the error show in the part:

if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
  http.send(null);
} else { // code for IE6, IE5
  http.send();
}

So, even if I use the onreadystatechange method cannot prevent the error...

The file is in a directoy beside my web pages.

Then what should I do to combine with httprequest?

Any advice appreciate.

Add:

I have used the method 'Head' but it is return 404... (No matter jQuery plugin/javascript)

Like the picture: enter image description here

What should I do...

Is the direction of the error I found misleaded?

2
  • Are you trying out the above example from your local hard disk or is the page hosted on a server and running over http protocol. If the sample is running from file:/// protocol, then you will not get readyState as 4 Commented Feb 25, 2014 at 10:31
  • It's in Linux server, thanks. Commented Feb 26, 2014 at 1:16

2 Answers 2

3

Try this function

function urlExists(testUrl) {
    var http = jQuery.ajax({
        type:"HEAD", //Not get
        url: testUrl,
        async: false
    })
    return http.status!=404;
}


//Usage
if(!urlExists('http://www.mysite.com/somefileOrImage.ext')) {
   alert('File not found');
}

HEAD

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification.

Read about head here

Sign up to request clarification or add additional context in comments.

1 Comment

async: false, synchronous operation in Firefox version 30.0 and later, and in recent/current versions of Chrome is deprecated due to unfavorable user experience. Attempted use results in fail/error. Should use async: true with callback function for asynchronous operation.
3

you can use this function

function UrlExists(url)
{
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
      var http=new XMLHttpRequest();
    } 
    else {
      var http=new ActiveXObject("Microsoft.XMLHTTP");
    }

    http.open('HEAD', url, false);
    http.send();
    return http.status!=404;
}

Reference

2 Comments

I have used this solution but I would recommend to include "http.send()" between try-catch to check if the url doesn't exists (I used a file and when the file wasn't in the directory passed as parameter then I got a javascript error)
async: false, synchronous operation in Firefox version 30.0 and later, and in recent/current versions of Chrome is deprecated due to unfavorable user experience. Attempted use results in fail/error. Should use async: true with callback function for asynchronous operation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.