Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-12-2011, 02:59 AM   PM User | #1
fedlerner
New to the CF scene

 
Join Date: Oct 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
fedlerner is an unknown quantity at this point
Not working in FF/Chrome, working in IE

Hi guys!

I've the following AJAX code:
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.-
fedlerner is offline   Reply With Quote
Old 03-12-2011, 09:47 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
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
Code:
if(window.XMLHttpRequest) {
   // modern browser
} else {
   // IE 6 or less
}
devnull69 is offline   Reply With Quote
Old 03-12-2011, 02:42 PM   PM User | #3
fedlerner
New to the CF scene

 
Join Date: Oct 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
fedlerner is an unknown quantity at this point
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:
Code:
/* ---------------------------- */
/* 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.-
fedlerner is offline   Reply With Quote
Old 03-12-2011, 06:10 PM   PM User | #4
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Are you running your script locally or via an actual web server?
devnull69 is offline   Reply With Quote
Old 03-13-2011, 12:13 AM   PM User | #5
fedlerner
New to the CF scene

 
Join Date: Oct 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
fedlerner is an unknown quantity at this point
On an actual web-server. I'll PM you the URL.
fedlerner is offline   Reply With Quote
Old 03-15-2011, 06:05 AM   PM User | #6
fedlerner
New to the CF scene

 
Join Date: Oct 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
fedlerner is an unknown quantity at this point
I've solved the issue on this script.
The thread can be closed.
fedlerner is offline   Reply With Quote
Old 03-15-2011, 12:20 PM   PM User | #7
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Have a heart and share the solution with us!
devnull69 is offline   Reply With Quote
Old 03-15-2011, 04:18 PM   PM User | #8
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Quote:
Originally Posted by devnull69 View Post
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)

Code:
http.open('get', URL, true);
2. Usually is useful to test the status of the document as well, not only the readyState property:

Code:
if(http.readyState == 4){
if(http.status==200){
...
}
}
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Reply

Bookmarks

Tags
ajax, firefox chrome, mysql, php, problem

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:35 AM.


Advertisement
Log in to turn off these ads.