How to fix the ajax request in IE and Firefox? - ajax

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.

Related

A wired URL, it can be accessed through browser, but can not be accessed through js or java programming

I have one wired url!
It can be accessed through browser, but can not be accessed through js or java programming.
If I copy and paste the UR into browser address, browser can get a string be returned, but I can not get it through javascript or java programming.
js code likes following:
var url="http://220.181.61.213/?prot=2&t=0.123123&file=%2Fv%2FHUGE%2FBackUp%2Fhadoop%2F20140118%2F1119469_1554068_v_H_120941_16856%2F1119469_1554068_v_H_120941_16856_001.mp4&new=%2F246%2F85%2FKE78lQ1a8pt6nefyEbSz.mp4";
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(data) {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
alert(xhr.responseText);
}
}
xhr.open('GET', url, true);
xhr.send();
No data be returned in above code, and I found NS_ERROR_DOM_BAD_URI when executed in firefox.
What is wrong?
You are trying to do a cross site Ajax request, check out this answer for an explanation:
Cross-Site Ajax Call

AJAX question: server is not returning proper status code in the event of a error

While designing a error handling mechanism for AJAX script on my website, I found that the only status code that was returned, by the server, in the event of a error was 0 (or "undefined"). Even when I intentionally created a 404 error by requesting a non-existent file, the only error code that was returned by the server was 0. I believe that this problem is caused by my web-host's server (www.000webhost.com) when it redirects 404 errors to http://error404.000webhost.com/? however, I need to find a way to get a proper error code from the server's response in order to deliver feedback to the user on what went wrong... So my question is: how do I make the server return the proper status code, or if that is not the problem: what is wrong with my 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) {
alert(xmlhttp.status); //this alert box shows 200 normally, but during a error only shows 0
if (xmlhttp.status==200) { // 200 = OK, process result
//stuff for processing the result (when there is no error)
}
else { // error handling (creates a jgrowl notification showing the status code)
$('#jGrowl-container').jGrowl('AJAX Error Code: ' + xmlhttp.status', {sticky: true, theme: 'error'});
}
}
}
xmlhttp.open("POST", "process.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("&s="+ScoutID + "&pw="+pword + "&rt="+RequestType + RequestText);
}
There could be different reasons why you are NOT getting proper http-status codes in response.
Check with your hosting provider whether they are handling or have customized the web server for this. You can check it in their control panel or ask their helpdesk.
Your server is redirecting in case of 404 which may not let the browser track it as 404 but instead a 302 or 303 (redirection). Few hosting companies do that.
Also I noticed that you are using jQuery already so why not use it for your ajax calls as well. It works well. You can check it out at http://api.jquery.com/category/ajax/

Ajax and IE8 Issue

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.

In IE, I can only send request to server 7 times maximum when using ajax

I added ajax to a web application with 7 'select' tags, in which selecting an option would populate the following 'select' tags with the related information. And some of these tags can also show another set of radio buttons or checkboxes. In all, you can make more than 10 requests to the server until you get your desired product.
All work fine in major browsers except in IE, where the request to the server are limited to 7 times and then nothing happens any more until you refresh the browser to start again. I even tried to disable the cache, but still the same problem occured...
Why is IE doing this nonsense?
This is the ajax code doing the server-client talking:
function updateAvailableAttributes()
{
var xmlhttp;
var form = document.forms["orderDefinition"];
form.elements["formChangeRequest"].value = "true";
var processingMsgBox = $("#waitingMsgOverlay, #waitingMsgBox, #waitingMsg, #waitingMsgParag");
var map = document.getElementById("OrderMap");
if(window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6 and below
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
switch(xmlhttp.readyState)
{
case 1:
map.disableApplication();
processingMsgBox.show();
break;
case 4:
if(xmlhttp.status == 200)
{
$('#usercontent .sleeve .toprow').html(xmlhttp.responseText);
applyValidation();
browserScrollManagement();
$("#toolpanel").height($("#orderMap").height());
inputBtnHighlightSelection();
}
else
{
sessionTimedOut();
}
$("#toolpanel").height($("#orderMap").height());
map.enableApplication();
processingMsgBox.hide();
break;
}
}
var parameters = $("form#orderDefinition").serialize();
xmlhttp.open("POST", "ajax/possibleValues.html", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", parameters.length);
xmlhttp.setRequestHeader("Cache-Control", "no-cache");
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(parameters);
}
The HTTP spec recommends that simultaneous connections to a web server must be limited. The limit is lower for IE7 than it is for other browsers. Modify the registry to increase it for testing purposes only.

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