AJAX in Internet Explorer 11 - Don't cache - javascript

I have an issue with AJAX in the IE 11. My page is asking sone values via AJAX form the server using this code:
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)
{
doSomeThing();
}
}
xmlhttp.open("GET", "theURL", true);
xmlhttp.send();
In Chrome and Firefox it's working fine but the IE seems to cache the AJAX response and I get the same result, even if the page on the server changed.
Is there a way to disable the caching?

Add a random parameter to the url, such as a timestamp:
var url="//yoururl.com/";
url+="?"+new Date().getTime();

This was driving me crazy. I tried many cache busting techniques and setting cache headers. So many of these either did not work or were wild goose chases. The only solution I found which tested to work correctly was setting:
Header Pragma: no-cache
I hope it saves others with IE headaches.
BTW this is StackOverflow thread is great to shed light on the difference between Pragma and Cache-control:
Difference between Pragma and Cache-control headers?

Related

xmlhttp.request fails to open local file

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 "";
}

Trigger webservice

I have a webservice that I can trigger to do various stuff by typing in the URL in a browser with some defined parameters:
Example:
http://hsserver/HomeSeer_REST_API.aspx?function=setdevicebyname&param1=bedroomlight&param2=on
I am building a web front end in plain HTML but cannot find a way to do this trigger without opening a new page in the broswer.
It would be preferable if it was a function like:
UPDATE: Got it to work with this code, but cant get the parameters to work (func,param1,param2) when I call it with
<button type="button" onclick="processnew('setdevicebyname','bedroom desktop light','off')">processnew</button>
function processnew(func,param1,param2)
{
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("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","HomeSeer_REST_API.aspx?function=setdevicebyname&param1=bedroom%20desktop%20light&param2=off",true);
xmlhttp.send();
I have no clue where to look for this - hoping for some help
XHR baby.
http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp
or jquery's abstraction [ajax generic, or post/get for shortcuts]
http://api.jquery.com/jquery.ajax/
well... that would be a simple HTTP GET request.
if you use jQuery, check this out: http://api.jquery.com/jquery.get/
if you want to stick to plain javascript (I wouldn't recommend that though due to some incompatibilities across browsers): http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp
Try jQuery
$.ajax({
url: 'http://hsserver/HomeSeer_REST_API.aspx?function=setdevicebyname&param1=bedroomlight&param2=on',
method: 'GET' // HTTP request method
}).done(function(data) {
console.log('Response from server: ' + data);
});

Read .svg file from server in javascript

I am working on svg and javascript. I want to read and display svg in html, svg is placed on my localhost and html is on another computer's localhost. Both are connected in LAN. I want to get svg file in javascript i try a lot but not success my code is as follows
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)
{
console.log(xmlhttp.responseText);
}
}
xmlhttp.open("GET","http://localhost/SVG/svg1.svg",true);
xmlhttp.setRequestHeader("Access-Control-Allow-Origin", "*");
xmlhttp.send(null);
it generates an error like this:
OPTIONS http://localhost/SVG/svg1.svg No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.1.118' is therefore not allowed access.
XMLHttpRequest cannot load http://localhost/SVG/svg1.svg. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.1.118' is therefore not allowed access.
I think the only way this will work is that in lieu of http://localhost/SVG/svg1.svg you determine the relative address of the
../SVG/svg1.svg

read txt file via client javascript

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.

Ajax call not working in IE7 and FF

I have one js file with a ajax call which is working fine in IE6, but not in IE7 or FF. Can somebody help?
window.onload = function() {
var xmlhttp;
var url = "myurl";
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
debugger;
alert("Hello");
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else {
alert("Your browser does not support XMLHTTP!");
}
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
alert(xmlhttp.responseText);
}
}
}
In IE7 I am getting access denied error. Please help.
EDIT:
I am now trying it using jQuery,
Code:
$(function() {
$.ajax(
{
type: "GET",
url: "myurl",
datatype: "html",
success: function(xhtml) {
$("#con").html(xhtml);
},
error: function() {
displayMessage(......);
}
});
});
Still its working in IE6 but not in Others.If its a cross domain issue, then how to solve this?
It might be some security issues. See if it works by adding all url's you use here to the trusted sites list.
IE6 has known bugs/issues when it comes to Javascript and cross-domain policy. This is why (amongst other reasons) that IE6 is no longer being supported in terms of cross-browser compatibility by many large organizations (why encourage something that has such a vulnerability?)
My guess, then, is that your var url = "myurl" is pointing to something on another domain or subdomain. But we need more details to be sure.

Categories

Resources