javascript - XMLHttpRequest detecting 404 (Not Found) - Stack Overflow

If the URL is correct (file.dat exists), this works great (the file length matches).If it is wrong I

If the URL is correct (file.dat exists), this works great (the file length matches). If it is wrong I will see a very small file length and I will not see the xhr.onerror.

How can I detect that the URL was incorrect?

var xhr = new XMLHttpRequest()
xhr.responseType = "blob"
xhr.onload = ()=> {
    var reader = new FileReader()
    reader.onload = evt => {
        var contents = new Buffer(evt.target.result, 'binary')
        console.log('file len',contents.length) 
    }
    reader.readAsBinaryString(xhr.response)
}
xhr.addEventListener("error", () => { console.error('xhr.onerror',e) })
xhr.open("GET", "file.dat")
xhr.send()

I do see a stacktrace in the console pointing to xhr.send() GET http://localhost:8080/file.dat 404 (Not Found)

A try catch around both open and send does not catch any exceptions.

Files are served by WebpackDevServer (I hope that should not matter though).

If the URL is correct (file.dat exists), this works great (the file length matches). If it is wrong I will see a very small file length and I will not see the xhr.onerror.

How can I detect that the URL was incorrect?

var xhr = new XMLHttpRequest()
xhr.responseType = "blob"
xhr.onload = ()=> {
    var reader = new FileReader()
    reader.onload = evt => {
        var contents = new Buffer(evt.target.result, 'binary')
        console.log('file len',contents.length) 
    }
    reader.readAsBinaryString(xhr.response)
}
xhr.addEventListener("error", () => { console.error('xhr.onerror',e) })
xhr.open("GET", "file.dat")
xhr.send()

https://developer.mozilla/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest

I do see a stacktrace in the console pointing to xhr.send() GET http://localhost:8080/file.dat 404 (Not Found)

A try catch around both open and send does not catch any exceptions.

Files are served by WebpackDevServer (I hope that should not matter though).

Share Improve this question edited Oct 23, 2015 at 18:23 jcalfee314 asked Oct 23, 2015 at 18:14 jcalfee314jcalfee314 4,8609 gold badges46 silver badges78 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 7

You can check the status of the response object.

// Not using arrow function because I don't want the lexical `this`
xhr.onload = function() {
    if (this.status === 404) {
       // not found, add some error handling
       return;
    }
    var reader = new FileReader()
    reader.onload = evt => {
        var contents = new Buffer(evt.target.result, 'binary')
        console.log('file len',contents.length) 
    }
    reader.readAsBinaryString(xhr.response)
}

Credit to https://developer.appcelerator./question/129410/xhr-request-cant-check-for-error-for-404-page-or-other-errors

Using https://developer.mozilla/en-US/docs/Web/API/XMLHttpRequest#xmlhttprequest-status:

XmlHttpRequest objects (you have one in the variable xhr) have a read-only property status that you can use to get the status text once it's loaded.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743753377a4501314.html

相关推荐

  • javascript - XMLHttpRequest detecting 404 (Not Found) - Stack Overflow

    If the URL is correct (file.dat exists), this works great (the file length matches).If it is wrong I

    6天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信