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?

Shuinvy
  • 251
  • 2
  • 6
  • 23
  • 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 – kcak11 Feb 25 '14 at 10:31
  • It's in Linux server, thanks. – Shuinvy Feb 26 '14 at 01:16

2 Answers2

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

Aamir Afridi
  • 6,364
  • 3
  • 42
  • 42
  • `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. – Kevin Fegan Nov 19 '17 at 20:57
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

Community
  • 1
  • 1
Satish Sharma
  • 9,547
  • 6
  • 29
  • 51
  • 2
    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) – Ignacio Rubio Oct 28 '15 at 15:00
  • 1
    `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. – Kevin Fegan Nov 19 '17 at 20:59