Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.-

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    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
    }

  • #3
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.-

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Are you running your script locally or via an actual web server?

  • #5
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    On an actual web-server. I'll PM you the URL.

  • #6
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've solved the issue on this script.
    The thread can be closed.

  • #7
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Have a heart and share the solution with us!

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •