...

View Full Version : Not working in FF/Chrome, working in IE



fedlerner
03-12-2011, 02:59 AM
Hi guys!

I've the following AJAX code:

/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}

var http = createObject();

/* -------------------------- */
/* INSERT */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;
function insert() {
// Optional: Show a waiting message in the layer with ID login_response
document.getElementById('insert_response').innerHTML = "Just a second..."
// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
var site_url= encodeURI(document.getElementById('site_url').value);
var site_type = encodeURI(document.getElementById('site_type').value);
// Set te random number to add to URL request
nocache = Math.random();
// Pass the login variables like URL variable
http.open('get', 'insert.php?site_url='+site_url+'&site_type=' +site_type+'&nocache = '+nocache);
http.onreadystatechange = insertReply;
http.send(null);
}
function insertReply() {
if(http.readyState == 4){
var response = http.responseText;
// else if login is ok show a message: "Site added+ site URL".
document.getElementById('insert_response').innerHTML = 'Site added:'+response;
}
}

This code works to insert a MySQL record to a Database using AJAX. It works fine on IE, but it's not working on FF or Chrome. When I test it on FF/Chrome, i just get the text "Just a second..." and it doesn't advance from there.
I don't really know AJAX, so I don't know where the issue is. I would really appreciate your help.

Regards,
Federico.-

devnull69
03-12-2011, 09:47 AM
The code itself doesn't seem to be wrong ... I would use a different approach for browser detection though. And I would use encodeURIComponent() instead of encodeURI().

Browser detection via feature detection


if(window.XMLHttpRequest) {
// modern browser
} else {
// IE 6 or less
}

fedlerner
03-12-2011, 02:42 PM
Hi devnull69,

Thanks for your help. Unfortunately, it's still not working. It works with IE, but not with FireFox or Chrome.

This is how the code looks like now:

/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject() {
var request_type;
var browser = navigator.appName;
if(window.XMLHttpRequest) {
request_type = new XMLHttpRequest();
} else {
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}
return request_type;
}

var http = createObject();

/* -------------------------- */
/* INSERT */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;
function insert() {
// Optional: Show a waiting message in the layer with ID login_response
document.getElementById('insert_response').innerHTML = "Just a second..."
// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
var site_url= encodeURIComponent(document.getElementById('site_url').value);
var site_type = encodeURIComponent(document.getElementById('site_type').value);
// Set te random number to add to URL request
nocache = Math.random();
// Pass the login variables like URL variable
http.open('get', 'insert.php?site_url='+site_url+'&site_type=' +site_type+'&nocache = '+nocache);
http.onreadystatechange = insertReply;
http.send(null);
}
function insertReply() {
if(http.readyState == 4){
var response = http.responseText;
// else if login is ok show a message: "Site added+ site URL".
document.getElementById('insert_response').innerHTML = 'Site added:'+response;
}
}

Do you know what could be going wrong?
Let me know if you want me to post the .html and .php files that work with this script.

Regards,
Federico.-

devnull69
03-12-2011, 06:10 PM
Are you running your script locally or via an actual web server?

fedlerner
03-13-2011, 12:13 AM
On an actual web-server. I'll PM you the URL.

fedlerner
03-15-2011, 06:05 AM
I've solved the issue on this script.
The thread can be closed.

devnull69
03-15-2011, 12:20 PM
Have a heart and share the solution with us!

Kor
03-15-2011, 04:18 PM
Have a heart and share the solution with us!
I think:

1. The open() method needs a third, Boolean, argument, which should control the type of the request: asynchronous (true) or synchronous (false)



http.open('get', URL, true);


2. Usually is useful to test the status of the document as well, not only the readyState property:



if(http.readyState == 4){
if(http.status==200){
...
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum