Ajax and IE8 Issue - ajax

I have been having a look to some post here related to the post Im writing now but I can not solve it.
The issue is that I have need some data from a php located in my server and make a call in the next manner:
function login(tipo) {
var xml = null;
try{
// Firefox, Opera 8.0+, Safari
xml=new XMLHttpRequest();
}catch (e){
// Internet Explorer
try{
xml=new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
try{
xml=new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
alert("Your browser does not support AJAX!");
return false;
}
}
}
xml.open("POST", "https://www.domoindal.com/mainSite/es/checklogin.php", false);
xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xml.send("user="+document.loginForm.user.value+"&pass="+document.loginForm.pass.value);
if(xml.status == 404) alert("Url no valida");
var respuesta = xml.responseText.split("#", 3);
..... and this code works fine in Safari, Chrome and Firefox, the only exception is IE8. It arises an error in the responseText line (the last one).
Another similar question related to IE8 is that I want to change the image in a div and I use the next code:
function boton_1() {
$("#contenedor_tarjetas").html( '<img src="../images/VISA.png" width="250" height="40" />' );
$("#cardID").value = 1;
return true;
}
.... it gives me an error in the second line. What happens with this browser?? It seems like if it needs a separated program.
I checked all kind of stuff and advices from other post with no success.
Is there anything missing? Whats wrong?
If someone could help me I would appreciate it a lot.
Thanks in advance.

At least I found the solution to the query I did. The fact is that there is a little problem with IE8 and the Ajax. When you make a query to a php in the server and you wait for an answer, the data is returned in a charset different than IE8 usually works. Its the only browser that has this problem.
So i solved it including in the PHP file a header in the next way:
header('Content-type: text/plain; Charset=utf-8');
So, if someone else has this problem, try with this.
I hope I could solved to anyone this problem.

Related

How to fix the ajax request in IE and Firefox?

i'm experiencing problems with testing my web application on firefox and internet explorer, the problem seem to be in the ajax calls made by my application to the server i realized this when i debugged my application using FIDDLER WEB DEBUGGER and i noticed that i don't get any response when im using IE or firefox.
I tried to change my request type from "GET" to "POST" and add a cache buster without any success.
Please peep my CODE:
this is where i create my ajax object:
function createXmlHttpRequestObject()
{
var xmlHttp;
if(window.ActiveXObject){
try{alert(0);
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
xmlHttp = false;
}
}
else{
try{
xmlHttp = new XMLHttpRequest();
}
catch(e){
xmlHttp = false;
}
}
if(!xmlHttp){
alert("Can't create object!!!");
}
else{
return xmlHttp;
}
}
and this is where i send the request:
function process(){
var params = "word="+word;
if(xmlHttp.readyState==0 || xmlHttp.readyState==4){
xmlHttp.open("POST","/gwizz/scripts/definition.php",true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length",params.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send(params);
}else{
setTimeout('process()',1000);
}
}
Any piece of help will be much appreciated.
#Moor
I can't answer exactly what is wrong but here are some pointers that may help you.
XMLHttpRequest - Perhaps this is a bit of topic but think its useful for more browser independent code.
I suggest using to create Ajax request and use ActiveXObject only in case you can't find XMLHttpRequest. Most browsers including IE8+ support this object so you have less code dependent on browsers. e.g. below copied from http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp
var xmlHttp;
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
Use Developer tools Firefox and IE modern versions come with very useful developer tools. Check the network sections and then perform clicks on your web application that are expected to invoke the ajax call. The full details of the http request actually fired are available in the network section. It will show you how the browser sees the request.
use console.log This question talks about some logging support which is available in IE. The same is also available in firefox. You should be able to pin point where your code execution fails. Does IE9 support console.log, and is it a real function?
If I were to take a guess, I would say the URL that is used to connect to the server may be resulting in 404.

Receiving AJAX HTTP Response Code as 0

I have a pretty simple AJAX and PHP code. While calling the PHP through the AJAX it receives the response code as 0. The PHP code is successfully run, but I can't get the response. What does this status '0' denote and how can I solve this?
function confirmUser(id)
{
xmlhttp=GetXmlHttpObject();
regid = id;
if (xmlhttp==null) {
alert ("Browser does not support HTTP Request");
return;
}
var url="confirm.php";
url=url+"?id="+id;
url=url+"&a=confirm";
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
$("#txtHint" + regid).text("Awaiting confirmation");
} else {
alert (xmlhttp.status); //this shows '0'
}
};
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
Well, this is the javascript I used. Pardon me if I should've added anything more than this. Also tell me what I missed.
I appreciate your help
GetXmlHttpObject function:
function GetXmlHttpObject()
{
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject) {
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
When working with XMLHttpRequests in the past, I've found that status 0 is usually returned for locally processed files. When I saw this question, I had a bit of a hunt around and found a confirmation of this at the following pages:
XMLHttpRequest - Why Status 0, and StatusText Unknown occur
https://developer.mozilla.org/En/Using_XMLHttpRequest#section_3
Here are the readyState codes for you.
0. Uninitialized
1. Set up, but not sent
2. Sent
3. In flight
4. Complete
(Source: http://www.stevefenton.co.uk/Content/Blog/Date/201004/Blog/AJAX-Ready-State-Codes/)
Do you get stuck constantly on a readyState of 0? If so, it means your request hasn't been sent, although I can see a line of code in your example "xmlhttp.send(null)"...
I would predict that you'll get a 0 before you call send, but after that a different status code. What happens if you wait a bit?
I know people may not want to hear it, but this is exactly what JS frameworks are for. Why mess with all of the various browser inclinations and disasters that are custom AJAX calls when you can just do a simple AJAX call through jQuery.
Basically, you are reinventing the wheel, and for no reason. Have your php return JSON data, and embed a variable in with the success code if you need to test for that.
<script src="jquery.js"></script>
<script>
$.get("myphp.php", { id : "yes", blah : "stuff" }, function(data) {
if (data.success == 1) {
alert("got data");
} else {
alert("didn't get data");
}
},"json");
</script>
Boom, you now have cross-browser AJAX.
This can happen if you're requesting an HTTPS resource and the handshake fails (for example an invalid certificate). In particular, if you're using the XML request object from outside a browser, the error may not be obvious.

ajax issue on IE8

I have an old site that uses xml documents, and when I created it i had firefox and IE7 to test on, and it worked just great. Since then, IE8 appeared, and it seams that the site does no longer work properly.
This is the current code:
if (window.XMLHttpRequest)
{
XMLHttpRequestObject = new XMLHttpRequest();
XMLHttpRequestObject.overrideMimeType("text/xml");
XMLHttpRequestObject.open("GET", "produse.xml", true);
XMLHttpRequestObject.onreadystatechange = function()
{
if (XMLHttpRequestObject.readyState == 4)
{
xmlDocument = XMLHttpRequestObject.responseXML;
removeWhitespace(xmlDocument);
}
}
XMLHttpRequestObject.send(null);
}
else if (window.ActiveXObject)
{
xmlDocument= new ActiveXObject("Microsoft.XMLDOM");
xmlDocument.async=false;
if (xmlDocument .readyState == 4)
{
xmlDocument.load("produse.xml");
}
}
But I get this error:
Message: Object doesn't support this property or method
Line: 19
Char: 3
Code: 0
which relates to:
XMLHttpRequestObject.overrideMimeType("text/xml");
What should I use instead?
This page shows what the differences are between firefox/ie/ie8 are and has some examples of how to do it:
http://www.javascriptkit.com/jsref/ajax.shtml
Mostly it says that IE8 does not support this method and you have to make sure your server is adding the proper header (text/xml) to the outgoing response.
It also has a link to http://www.javascriptkit.com/dhtmltutors/ajaxgetpost3.shtml that explains the common pitfalls and IE problems.
It says do:
if (mygetrequest.overrideMimeType)
mygetrequest.overrideMimeType('text/xml')
jQuery would be my choice. It is a lot faster and is cross browser supported and on a CDN.
You could strip that code down to just a few lines.
$.get('ajax/test.html', function(data) {
$('.result').html(data);
alert('Load was performed.');
});

Problem running my project in IE 6.0 and 7.0 (window.location)?

My project is running perfectly in Firefox, google chorme and IE 8.0
But it is not working on IE 6.0 or 7.0
I realized that it is given problem at window.location
I am placing my code over here to show what i am doing.
function GetEmailId()
{
var url="http://server.com/GetPostEmail.php";
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=statechangedLogin2;
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
}
function statechangedLogin2()
{
if(xmlhttp.readyState==4)
{
if(xmlhttp.responseText=="Login again")
{
window.location="http://server.com/profile.html";
}
}
}
So this code is working fine in other browsers except for IE 6 and 7.
When i get the response from my AJAX in xmlhttp.responseText it should go over to profile.html rather in IE 6 and 7 it stays back on the original page where i was before that is qotw.html.
i think there is something wrong with the window.location, probably i need a different command here.
Also my GetXmlHttpObject looks like this.
function GetXmlHttpObject() {
//var xmlHttp = null;
try {
xmlHttp = new XMLHttpRequest();
} catch (e) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
Please if anyone can help me with this problem of mine.
Regards
zeeshan
Note: I again tried to decode my code and realized that in IE 6 and 7 my code never goes into statechangedLogin2(). And that is the reason my code is not working. But why is that happening as the code it working fine in other browsers even in IE8?
The location property is an object, the url is the href property in that object. Use this instead:
window.location.href="http://server.com/profile.html";

ajax request to download an excel file is showing me truncated response

I am trying to download an Excel file using Ajax (XMLHttpRequest).
On completion the responseText is found to have just 5 characters.
The network sniffing tool (Fiddler) is showing me that my computer received the entire file..
so why is the responseText showing me only 5 characters? I have tried both Synch and Asynch calls.
Thanks for any help you can give here.
var xmlHttpReq = getXmlHttpRequestObject();
function getXmlHttpRequestObject(){
var xmlhttp;
if (window.XMLHttpRequest){// code for all new browsers
xmlhttp=new XMLHttpRequest();
}else if (window.ActiveXObject){// code for IE5 and IE6
// xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
progids = ['MSXML2.XMLHTTP.6.0', 'MSXML2.XMLHTTP.3.0', 'Microsoft.XMLHTTP'];
for (i=0 ; i < progids.length; i ++){
try {
xmlhttp = new window.ActiveXObject(progids[i]);
break;
} catch (e) {
//do nothing
}
}
}
return xmlhttp;
}
//utility method for http get
function doSynchronousGet(url){
if(xmlHttpReq == null){
xmlHttpReq = getXmlHttpRequestObject();
}
//change last param to true for making async calls.
xmlHttpReq.open("GET" ,url,false);
xmlHttpReq.setRequestHeader("Connection", "close");
xmlHttpReq.send(null);
return xmlHttpReq.responseText;
}
var resultText = doSynchronousGet(url);
alert('resultText length: '+ resultText.length);
alert('resultText: '+ resultText);
The issue is probably that XMLHttpRequest doesn't ususally take binary data like an Excel file. If you just want to let the user download the file, read Ramiz's post. If you need to read the data in JavaScript, try switching to a text format like CSV (or better for parsing, JSON). If you really need to read a binary file, there are discussions of that here and here.
Do not use Ajax call (xmlhttprequest) to download/upload files. Better do it on server side.

Resources