This question has been asked twice on these forums, but the answer provided is not working for me.
The issue is that I have JSP page which is returning and flushing small amounts of output.
I am using the following code to read the output:
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 3) {
alert(xmlhttp.responseText);
}
}
xmlhttp.open("POST", "download.jsp", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader('X-Requested-With', "XMLHttpRequest");
xmlhttp.send($('#submitDownloadForm').serialize());
On Firefox this works fine, and I am given 3 alerts during the duration of the process.
However, on Webkit based browsers such as Chrome and Safari, I am given the first alert, but not the other 2 until the process has completed.
Other answers have said that changing the Content-Type:text/plain or Content-Type:application/octet-stream, but if I do this, the readyState jumps straight to 4 as if the process has completed instantly.
I cannot find any solutions for this.
Any help greatly appreciated, thanks in advance everyone.
I added the following code to the JSP file just before where it produces the output, and this has resolved the issue on Webkit-based browsers:
response.setContentType("application/octet-stream");
I'm now getting the updates every time output is flushed.
Related
Apologies if this question is a bit vague, but I have no idea how to make it more specific. I am a beginner when it comes to javascript. I have a web page which sends a number of ajax calls to the server to call php scripts which runs some sql against a mysql database. Everything was working fine then all of a sudden I started to get an internal server error 500 on one of my ajax calls. The ajax call is placed in a function, it works most of the time. Only when I pass the function one specific sql query does it give the error. I've tested the query in my database manually and it works so its not an issue with the query that I am passing. The strange thing is, since the error has started, every now and then it does work, however most of the time it is not working, the error seems to be somewhat random. I realise that with limited information it may be impossible to give an exact answer, but has anyone experienced anything like this before? OR does anyone have any clues as to how I can get to the bottom of this. I've tried everything. I will paste the function that is causing the error (not sure if that will help at all). The error comes on the "xmlhttp.send();" line.
function phpRequest2(prov, phpsc, funct, bolL) {
<!-- document.getElementById("demo").innerHTML = qry; -->
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");
}
document.getElementById("demo").innerHTML = prov;
xmlhttp.open("GET", phpsc + ".php?qry=" + prov, true);
xmlhttp.send();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var arrLocs = JSON.parse(xmlhttp.responseText);
funct(arrLocs, bolL);
}
}
}
You best bet at finding out the reason when you get the Error 500 is to edit your php.ini file and change the following parameters
display_errors = Off
display_startup_errors = Off
to
display_errors = On
display_startup_errors = On
Hopefully that should give you exact details on what your error is and thereby help you solve it.
I've searched and searched and could not find a solution for this:
I'm trying to load .txt file contents with Ajax but the file comes back empty.
I checked the ready state and status. ready state is 4 which means that connection was set, data received , status is 0 which means error for loading http but I read on a forum that it is ok for a local file.
I really don't know what to do.
all help will be appreciated!
this is my code:
<script>
function load() {
var selectedValue = document.getElementById("mySelect").value;
var xhttp;
alert("in func")
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
try{
xhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){
try{
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
}
}
}
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && (xhttp.status == 200 || xhttp.status == 0)) {
document.getElementById("textDiv").innerHTML = xhttp.responseText;
alert(xhttp.responseText);
}
};
if(selectedValue == "Year 1"){
xhttp.open("GET", "file://year1.txt", true);
xhttp.send(null);
}
if(selectedValue == "Year 2"){
xhttp.open("GET", "year2.txt", true);
xhttp.send(null);
}
if(selectedValue == "Year 3"){
xhttp.open("GET", "year3.txt", true);
xhttp.send(null);
}
}
</script>
The issue is most likely the file protocol: file://year1.txt
try using a relative path instead, ie ./year1.txt
Edit..
I missed this the first time around.. you will get several hits to your callback function on a successful call. You'll get one where the status is zero first, then if successful you'll get another one with a 200. Why are you checking for both?
change this...
if (xhttp.readyState == 4 && (xhttp.status==200 || xhttp.status==0))
to this...
if (xhttp.readyState == 4 && xhttp.status==200)
OK! after EXTENSIVE research the problem was the infamous cross origin request problem (which does not allow access to local files as part of a security measure to insure you don't upload infected files &ct)
the solution is :
when initializing the variable for the XMLHttpRequest write:
xhttp = new XMLHttpRequest({mozSystem: true});
(notice the {mozSystem: true} inside the request)
AND IT SOLVES THE PROBLEM!!!
Thank you for your time, I hope this helps someone :)
My previous answer worked but not for long - after I restarted my computer the ajax stopped working at started throwing the cross origin error again. so- the best option I found that also works for all the browsers I checked (chrome, IE), is to set up a local server and run it from there.
here is a link for instructions:
https://www.maketecheasier.com/setup-local-web-server-all-platforms/
Good luck!
I am new to javascripts
please help me to solve this,
I have a function for db trip. that fires on every 10 mili seconds. problem is data is varying some time it gives 3 records sometime 5 records. there is not an issue of sp parameters I have passed. I think it's due to the function call which is not a thread. so process of function call is overlapping before the previous call complete
I have seen article for multi threading
https://gist.github.com/johdax/1269740
but don't have idea to integrate my function with threading.
this is my function
<script>
setInterval(function(){UserList()},10);
function UserList()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp6=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp6=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp6.onreadystatechange=function()
{
if (xmlhttp6.readyState==4 && xmlhttp6.status==200)
{
$("#UserStatusList").html(xmlhttp6.responseText);
}
}
var a = $('#cmbProjectList').val();
if (a==null){ a=""}
xmlhttp6.open("GET","UserList.asp?ProjectId=" + a,true);
xmlhttp6.send();
return false;
}
please help me to solve this.
how can I apply threading on this?
Browsers don't really use threads in any exposed way, AFAIK. If you wanted something truly off the normal browser flow, you could look into the Web Worker API which makes a separate sandbox for JS activities.
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.
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.