WebView access to HTTP Status Code
-
Hello,
I'd like to know when my WebView gets a 404 or otherwise fails to load a web page. If the URL is malformed, I can use onLoadFailed, but I don't know how to catch a 404 and respond in QML. Is this possible?
@
import Qt 4.7
import QtWebKit 1.0WebView {
width: 640
height: 480
url: "http://www.google.com/page/does/not/exist.html"onLoadFailed: { console.log("Page failed to load: " + url) }
}
@
Thanks,
Matt -
WebView is quite limited and I'm afraid you can't do that without using any tricks. Here is one of the tricks: using XMLHttpRequest to request the same url inside onLoadFailed and catch the status code, e.g, something like this:
@
onLoadFailed: {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.status == 404) { print('URL not found: ' + url) }
}
xhr.open("HEAD", url, true);
xhr.send(null);
}
@Cheers,
Phi -
Hi,
I'd recommend adding a suggestion in the bug tracker for this -- so hopefully it will become easier to handle in future versions.
Regards,
Michael -
I will give that XMLHttpRequest a try. Thanks for the suggestion!
"http://bugreports.qt.nokia.com/browse/QTBUG-17405":http://bugreports.qt.nokia.com/browse/QTBUG-17405