The following javascript used to work but now does not. xmlhttp.status is 0. The file "SBL_Stats.htm" resides in the same directory as the javascript.
I'm using firefox.
Can anyone help me out?
var filePath = "SBL_Stats.htm";
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
Log("Firefox");
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
Log("IE6");
}
xmlhttp.overrideMimeType('text/plain'); // don't scan for html
xmlhttp.open("GET",filePath,false); // false means sync request
xmlhttp.send(null);
if (xmlhttp.status != 200)
{
Log("get_SBL_Stats_Data failed: " + xmlhttp.status);
return "";
}
Thanks to dandavis who started me down the right track. The problem occurs because I was using local files. I guess that means using the file:: protocol as opposed to the the http:: protocol. No status is returned when accessing local files. Removing the status check corrects the problem. However I wanted to be able to detect if the file could be opened so I added a try catch block around the send call as follows...
try
{
xmlhttp.send(null);
}
catch(err)
{
Log("xmlhttp.send error " + err);
return "";
}
Related
First one XMLHttpRequest is calling script to update MySQL database it can run multiple times depending on the situation. Script insertLocation will do the job, but in the end I am calling second XMLHttpRequest which should update file made from database. The problem is the second XMLHttpRequest will never read from updated database so there are data missing from first XMLHttpRequest.
However when I look to database, the data are there. I tried even sleeping before the creating of the file, but it did not helped. It seems like calling 2 XMLHttpRequest is not possible. I know about that third parameter of .open() , that when its set to false it will wait for request to be done, but it is now working. Here is my code:
insertToDatabase(lat,lng,id);
//there I tried 10 second sleep
updateDatabaseFile();
function updateDatabaseFile() {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","updateCoords.php",false);
xmlhttp.send();
alert("updated");
}
function insertToDatabase(lat,lng,id) {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","insertLocation.php?lat="+lat+"&lng="+lng+"&id="+id,false);
xmlhttp.send();
alert("inserted")
}
One lats thing, when I run the updateDatabaseFile.php manually it will do the job. So I think the problem might be that changes done to database are made when this whole script ends so that is why I am getting incomplete data when reading.
EDIT: the alert("inserted") will open after the alert("updated") so there is the fail. The second one will finish first.
I have a js/ajax script at http://worldcitiesdatabase.info/test1/
I just received a complaint that it does not work properly in IE8.
Onchange seems to work, but then the next menu is not populated.
Can you please help me figure it out?
Thanks
Not sure what the problematic part of the code is. Here is my guess:
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
newList="";
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
newList = xmlhttp.responseText;
m1.innerHTML=newList;
m1.disabled=false;
}
if (menuname=="showCountry") {
var c1=document.getElementById('showRegion');
if (c1.options.length==2) {
if (c1.options[0].value=='NONE') {
c1.remove(0);
c1.value='0';
reloadmenu(c1);
}
}
}
}
xmlhttp.open("GET",newFile+".php?q="+menuvalue,true);
xmlhttp.send();
The line
m1.innerHTML=newList;
is the culprit because you use innerHTML to add <option>s to <select>. This is a known bug in IE8 - http://support.microsoft.com/kb/276228.
Note: You can run your test page in your Internet Explorer if you just open Developer Tools and run it in IE8 mode (see for example http://techathlon.com/internet-explorer-10-run-compatibility-mode/)
I have the following code placed into a function and when the function is called it loads my XML file and displays it in a message box:
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);
} else
{
alert('Panel not communicating.Reason: '+xmlhttp.status);
}
}
xmlhttp.open("POST","myfile.xml",false);
xmlhttp.send();
The above code does everything like it's meant to.
However, as soon as I add the following code to the top the page:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
when I load my function it comes up with the following message:
Panel not communicating.Reason: 200
however it still loads my XML file like how I want it to.
After searching around it says that xmlhttp status 200 means "OK"
Does anyone know why it's displaying this message?
It probably means that xmlhttp.readyState is transitioning to a state other than 4 ("complete") -- this can occur if the whole page doesn't download instantly, which is likely. Your function should probably return immediately when xmlhttp.readyState != 4, rather than treating that as an error condition.
You should also strongly consider using a Javascript framework such as jQuery here, as it will save you a lot of unnecessary effort. For instance, all of the code you've got here can be reduced to:
$.post("myfile.xml", function(data) {
alert(data);
}).error(function() {
alert("Panel not communicating.");
});
I think the code you added is not supposed to appear in XML, it was for HTML, so the browser may fail to parse the file, which made the xmlhttp.readyState==2, I am not sure for this value, may you check it.
I have a wordpress site
for ex, www.test.com
There is a js file (forms.js)in the script folder of mytheme folder.
ie (theme/mytheme/script/forms.js)
There is a mail.php page in mytheme folder
ie (theme/mytheme/mail.php)
Following is the content of forms.js
function submitFormToEmail()
{
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)
{
//document.getElementById("results").innerHTML=xmlhttp.responseText;
alert("Form received, thank you!");
}
}
xmlhttp.open("GET","/mail.php",true);
xmlhttp.send();
}
I will call "submitFormToEmail",if we click a image in the page *www.test.com/hello_test*
Where "hello_test" page lies in theme/mytheme/.
But the mail.php is not working.
Let me know ,how can we set the path of mail.php in the function "submitFormToEmail", so that it will work
Calling /mail.php will look for the mail.php file at http://www.test.com/mail.php. Just fix the path?
Either directly code the correct path into the javascript file or use php to dynamically get it using get_bloginfo. There are several ways to do this, but probably the best way is to create a hidden element on the page that contains the url and then fetch this when building your ajax request. Otherwise you'd have to set php to parse js files and include it in there. Doesn't seem like a good way to me though.
get_bloginfo('template_url');
I'm new to javascript and trying to open a txt file into var and then inject it to html div...
I tried to use fopen but I didn't succeed.
<script type="text/javascript">
file = fopen(getScriptPath("info.txt"), 0);
file_length = flength(file);
var content = fread(file,file_length);
var div = document.getElementById("myDiv");
//alert(div);
div.innerHTML = "";
div.innerHTML = content;
</script>
Although it says xml request this works perfectly fine for txt files too (server and client side).
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","YOUR_FILE.txt",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseText;
JavaScript has none of the functions you are trying to use.
To read files on the server in JavaScript, you can use XMLHttpRequest.
There is no easy way to read files on the client machine.
abandoned question:
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","YOUR_FILE.txt",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseText;
by Freek8
For security reasons, Javascript is made so you can not do this. However, a person has made a workaround which may work and posted it here.
Ok, I realize, it only works for files that are publicly accessbile on the server, which I believe is not what you want to do. Still, if you do find a way, it will be a hack like this, but it could also be fixed to not work at any time.