Internet Explorer error message "The operation was timed out" - javascript

I have a piece of code that works in chrome and firefox but not in internet explorer. I can't figure out what that actual cause is. I get a operation timeout message from internet explorer "Message: The operation was timed out."
This is the ajax function I am using, it's from w3schools so I know this is correct.
function ajax() {
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
alert(xmlhttp);
return xmlhttp;
}
This is the code that's getting stuck on. The error message is in "ajaxRequest.send(postdata);".
function edit(){
var ajaxRequest = ajax();
var postdata = "data=" + document.getElementById("id1").value + "<|>" + document.getElementById("id2").value + "<|>" + document.getElementById("id3").value;
ajaxRequest.onreadystatechange = function(){
var ajaxDisplay = document.getElementById('ajaxDiv');
if(ajaxRequest.readyState == 4 && ajaxRequest.status==200){
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
alert(postdata);
ajaxRequest.open("POST","confirmPage.php",false);
ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
ajaxRequest.send(postdata);
alert("Finished");
}
All the other pages work with the same code in internet explorer but not this particular page. I can't seem to figure to out why. This page works in chrome and firefox but not in internet explorer. It never goes to "Finished". I am using IE 8.

As you require synchronous behaviour try the following:
ajaxRequest.open("POST", "confirmPage.php", false);
ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
ajaxRequest.send(postdata);
if (ajaxRequest.status === 200) {
document.getElementById('ajaxDiv').innerHTML = ajaxRequest.responseText;
}
alert("Finished");
You don't need the onreadystatechange as the request is synchronous.

Partial answer:
I figured out the problem. It's not javascript and/or ajax. IE can't handle a large number of results in queries so it times out. It's a very obscure error as I was thinking it's something to do with the ajax functions rather than the php file.
The result set is not huge. There are 5 different queries. Each one with about 5-50K records(I am not printing all of them, just querying). It times out after a large result set.
To test I created a test page with simple SELECT * queries and it can handle only 2-3 queries. If it's more than that it times out.

Related

javascript get html file from online link

i want to call a html online link using xmlhttprequest with javascript, here is my code
but when the code arrive to xmlhttp.open it stopped and does not continue the execution
function loadXMLDoc(size,downloadfromurl) {
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var temp = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "http://app.arsel.qa/mobileappspeedtest/samples/256.htm?n=" + Math.random(), false);
xmlhttp.send(null);
}
What you are doing is an actual AJAX request to that page.
Cross domain AJAX requests are not allowed by default for security reasons.
However, there are several ways of performing cross domain requests, and you could take a look at how jQuery does it, so you don't have to reinvent the wheel all over again using plain JavaScript. This article should be helpful.
Anyway, if you actually want to crawl that page, there are tons of open source libraries for server side scripting languages like Java, PHP, Node.js, etc that are very useful in gathering the content, parsing the HTML and so on, depending on your needs.
You can use JSONP for overcoming cross domain barrier.
$.ajax({
type:'GET',
dataType:'jsonp',
jsonp: "jsonp",
url:"http://yoururl.com?callback=callbackFunction"
});
function callbackFunction(data){
//you can process the data here
console.log(date)
}

Javascript - Reading a text file line by line. Does it matter what browser is being used?

I have just started getting into Javascript and mobile web programming. One thing that I am uncertain of is how I can write proper code that runs on any browser without having the end user have any extra requirements on their end (what browser to use).
I am coding this in google chrome and more recently in c9.io.
I thought this would work:
function readTextFile(file)
{
var client = new XMLHttpRequest();
client.open('GET', file);
client.send();
client.onreadystatechange = function() {
alert(client.responseText);
}
}
But i get the error that XMLHTTpRequest is not defined. I have been trying to figure out why this is and I keep coming to different browsers not supporting this. I had figured simple file io would not be that difficult but its causing me more trouble than I had hoped.
What is the best way to input a text file? It is 1 text file that is not having anything being written to it. Just read only. The end user isn't choosing this text file it should be the only option.
The order in your code is wrong, send method should be the last one; otherwise, your code is fine and it should work fine in all modern browsers. The mentioned order issue, or maybe was something else (before) causing that error. The snippet below will also split received text in an array of text lines
var xhr, i, text, lines;
if(window.XMLHttpRequest){
// IE7+, Firefox, Chrome, Opera, Safari
xhr = new XMLHttpRequest();
}else{
// IE5, IE6 - next line supports these dinosaurs
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
text = xhr.responseText;
lines = text.split("\n");
for(i = 0; i < lines.length; i++){
console.log(lines[i]);
}
}
}
xhr.open('GET', 'http://domain/file.txt', true);
xhr.send();
XMLHTTpRequest is not supported by the older browsers. Try doing this to support the older browsers as well:
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

Show Save-As window from JSP

I need to write a String in a file on the client side, however as the Internet protocol does not allow that for Security concerns, this is the workaround I did: I have an AJAX request which invokes a JSP that queries a Database to get a String. I need to show the users a "Save-As" dialog and write this String to the local path they specify.
My JavaScript function:
function openReport(id)
{
var url = "../reports/reportsHandler.jsp?id=" + id;
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//alert(xmlhttp.responseText);
alert("result obtained");
}
}
xmlhttp.open("POST", url, true);
xmlhttp.send();
}
In the JSP, I have something like this:
response.setHeader("Content-Disposition", "attachment;filename=report.xml");
out.println(stringObtainedFromDatabase);
I do not see a Save As dialog while I get the alert saying result obtained. This is the first time I am doing this, could you please tell me if I am doing something wrong?
But, is there a way in JavaScript to show users a Save-As dialog and write the content of "div" tag in a file on the Client system?
Use a regular HTTP request, not an AJAX (XMLHttpRequest) one.
function openReport(id)
{
var url = "../reports/reportsHandler.jsp?id=" + id;
window.location = url;
}
This will send an HTTP GET, not a POST, though it looks like GET is the correct HTTP method to use here anyway, since you're retrieving data and not actually altering anything on the server.

XMLHttpRequest status 0 on second load

I am experiencing an interesting issue when I am trying to load some data in .txt format from the same domain using XMLHttpRequest.
I am trying to load the data, parse it and then store it in localStorage
var xmlhttp;
// code for IE7+, Firefox, Chrome, Opera, Safari
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var temp;
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
temp = xmlhttp.responseText;
}else{
alert("readyState: " + xmlhttp.readyState + "status: " + xmlhttp.status);
}
}
xmlhttp.open("GET","data/somedata.txt", false);
xmlhttp.send();
This code only works if I clean the history and cache; however, on second click of the same link, I would received "Readystate: 4, status 0" for some reason.
Does this has anything to do with localStorage?
if (!localStorage.somedata || localStorage.somedata.count(':') !== somedata.count(':')) {
localStorage.somedata = temp;
}
window.somedata = JSON.parse(localStorage.somedata);
There are two causes of status code of zero.
Making calls from the file protocol.
The page is refreshing/navigating away as the request is being made.
In your case I would assume it is #2. If you are using a button or a link to make the Ajax call, make sure to cancel the click action with either preventDefault or return false.
Sounds like a caching issue. Try either switching to a POST method, or appending a timestamp to the GET request querystring and see if that prevents the caching.
xmlhttp.open("POST", "data/somedata.txt", false);
or:
xmlhttp.open("GET", "data/somedata.txt?" + new Date().valueOf(), false);
Edit: If those don't work, modify your server configuration to send appropriate response headers for that file or type to not cache the response. Ie: Cache-Control: no-cache
Try xmlhttp.abort() before opening a new request.
It's a long shot but worth the try.

IE issue with DOM manipulation

I have a AJAX form and I use this line on receiving a response:
document.getElementById("output").innerHTML = xmlhttp.responseText;
Output is a div and in IE I'm getting an Unknown JavaScript Error.
Would it be the content that's being passed from the AJAX that's causing this error or is there something syntactically wrong with that line?
EDIT:
if(valid==true){
//AJAX
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}else{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("output").innerHTML = xmlhttp.responseText;
id = document.getElementById("parentID").value;
}
}
var parameters = "shedloadofvariables"+shedloadofVariables;
xmlhttp.open("POST", "register.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(parameters);
}
else{
alert("Please Fill in All Fields");
}
Cheers
Output is a div and in IE I'm getting an Unknown JavaScript Error.
"Unknown runtime error" commonly occurs when setting invalid HTML via the innerHTML property. Not all invalid HTML will cause this problem — the common case is trying to stuff a block element into an element that doesn't allow block elements, like a <div> inside a <p>. Only IE spits out this error message, other browsers will do their best to recover from your crappy HTML.
First thing to do is validate the HTML with the W3C validator. For more information, take a look at http://blog.rakeshpai.me/2007/02/ies-unknown-runtime-error-when-using.html.
You could try using jquery $('#output').html(xmlhttp.responseText);
I still don't understand the problem fully (Andy E's answer certainly helps though).
I found a work-around for this issue:
var t = document.createElement('div');
t.innerHTML = xmlhttp.responseText;
document.getElementById("output").appendChild(t);
Flawless.
Don't know why, but I'm not about to question it, because it works!

Categories

Resources