admin管理员组

文章数量:1332865

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 bine 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:

What should I do...

Is the direction of the error I found misleaded?

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 bine 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:

What should I do...

Is the direction of the error I found misleaded?

Share Improve this question edited Feb 26, 2014 at 1:56 Shuinvy asked Feb 25, 2014 at 10:25 ShuinvyShuinvy 2512 gold badges6 silver badges25 bronze badges 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 – kcak11 Commented Feb 25, 2014 at 10:31
  • It's in Linux server, thanks. – Shuinvy Commented Feb 26, 2014 at 1:16
Add a ment  | 

2 Answers 2

Reset to default 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./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

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

本文标签: