IC SunsetThe developerWorks Connections platform will be sunset on December 31, 2019. On January 1, 2020, this forum will no longer be available. More details available on our FAQ.
Topic
  • No replies
stevenhoi
stevenhoi
1 Post

Pinned topic AJAX works in IE but not Firefox

‏2011-04-18T09:50:10Z |
Hi I am on Firefox 3.6.12...
I have an AJAX function that works in Internet Explorer, but not on Firefox.

The setup goes like this:
There are 2 servers. Server1 and Server2
The javascript is located in Server1 and the servlets are located in Server2.
A proxy server is setup at Server1 to redirect requests to Servlets at Server2 to solve the cross-domain issue.

POST method is used.
In the IE, response is received properly (plaintext).
However, in FF, the request header is mysteriously changed to OPTIONS and there is no response from the server whatsoever.

I have tried installing the CORS filter to enable all cross domain request on the server but it doesn't seem to help. Can anyone help me?
The ajaxFunction() is called from within the webpage. Here is a snippet of my javascript file:

=========================================================================

var txtname = "";
var txtvalue = "";
function getXMLObject() //XML OBJECT
{
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP") // For Old Microsoft Browsers
// document.write("Old Microsoft Browsers");
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") // For Microsoft IE 6.0+
// document.write("Microsoft IE 6.0+");
}
catch (e2) {
xmlHttp = false // No Browser accepts the XMLHTTP Object then false
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest(); //For Mozilla, Opera Browsers
// document.write("Mozilla, Opera Browsers");
}
return xmlHttp; // Mandatory Statement returning the ajax object created
}

var xmlhttp = new getXMLObject(); //xmlhttp holds the ajax object

function ajaxFunction() {
xmlhttp = new getXMLObject();
//if(xmlhttp) {
txtname = document.getElementById("SearchInput");
txtvalue = txtname.value;
//alert("AJAX function - txtvalue: " + txtvalue);
if (/\S/.test(txtvalue)){ //check if textbox is not empty or just contains whitespaces
var url = 'http://msm2.cais.ntu.edu.sg:8095/dmserver/TrieQuery';
xmlhttp.open("POST",url,true); //TrieQuery is the servlet name
xmlhttp.onreadystatechange = handleServerResponse;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
//xmlhttp.send("query=" + txtvalue); //Posting txtname to Servlet
xmlhttp.send("query=" + encodeURI(encodeURI(txtvalue)));

/* xmlhttp.open("GET","TrieQuery?query="+txtvalue,true);
xmlhttp.onreadystatechange = handleServerResponse;
xmlhttp.send(null); */
}
//}
}

function handleServerResponse() {
if (xmlhttp.readyState == 4) {

//alert("xmlhttp status" + xmlhttp.status);
//alert(xmlhttp.status);
if(xmlhttp.status == 200 || xmlhttp.status == 0) {
// document.myForm.message.value=xmlhttp.responseText; //Update the HTML Form element

//document.getElementById('productsPreview').innerHTML=xmlhttp.responseText;
sgn_states.length=0; //clears last suggestion array
sgn_hits.length=0; //clears last hit array
sgn_previewDetails.length=0; //clear preview details of last query
//suggestions delimited by "~"
//other details delimited by ":"
//e.g. nike:30,adidas:117,puma:6
var tempStates=xmlhttp.responseText;
//alert("nth");
// document.getElementById('test').innerHTML=xmlhttp.responseText;

var mySplitResult=tempStates.split("~");
for (i=0;i<mySplitResult.length;i++){
var tempItems=mySplitResult.split(",");
sgn_states
=tempItems[0];
sgn_hits=tempItems[1];
sgn_previewDetails
="";
for (j=2;j<tempItems.length;j++){
sgn_previewDetails=sgn_previewDetails + tempItems[j] + ",";
}
}

//loads suggestions
//alert("handleServerResponse function - txtvalue: " + txtvalue);
requestSuggestions(txtvalue, false);

//should display only if user selects option?
//displayProductPreview(0);
}
else {
alert("Error during AJAX call. Please try again");
}
} else {
//alert("ReadyState !=4 ");
}
}

function displayProductPreview(index /*to select which suggestion item to preview*/){

var toPrint = "";
var tempDetails=sgn_previewDetailsindex.split(",");
if (tempDetails=="," || tempDetails==null) toPrint="No preview available.";

for (i=0;i<(tempDetails.length/3)-1;i++){ //Each item has 3 attributes thus /3
toPrint += '
';
toPrint += '
i*3+2 + '">i*3+1 + '" width="100px" height="100px"/>
';

//toPrint += '
' + tempDetailsi*3 + '
';

toPrint += '
';

//alert("HTML- " + previewHTML);
//print row tag for start of every row of 5
// if (i%5==0) previewHTML=previewHTML + "<tr>";
// previewHTML=previewHTML+ "<td>i*3+2 + "">
" + tempDetailsi*3 + "</td>"
//print row closing tag for end of every row of 5
// if (i%5==4) previewHTML=previewHTML + "</tr>";
}
//print row closing tag if table does not end exact at row of 5
//if (tempDetails.length%5!=0) previewHTML=previewHTML + "</tr>";
//previewHTML=previewHTML + "</table>";

//alert("Enter\n:" + previewHTML);
$(".imageSuggestion").html(toPrint);
//document.getElementByClass('sub').innerHTML=previewHTML;
}

=========================================================================