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 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    79
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Problem with ajax

    I have the following ajax code:

    .............
    .............
    .............
    XHR.createXMLHttp();
    XHR.open("POST", "/cgi-bin/addpost.cgi", true);
    XHR.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    XHR.onreadystatechange = function() {
    if (XHR.readyState == 4) {
    if (XHR.status == 200) {
    alert("Operazione effettuata con successo");
    }
    else {
    alert("Operazione non riuscita");
    }
    }
    }
    XHR.send(url);
    The function send data, but it don't return never alert windows. Where is the problem?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,031
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    Who knows? You don't show what that XHR object looks like.

    You don't show what the url variable contains.

    Maybe the AJAX call isn't working at all.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Jan 2010
    Posts
    79
    Thanks
    1
    Thanked 0 Times in 0 Posts
    The full code is:

    function createXMLHttp() {

    if (typeof XMLHttpRequest != 'undefined') {
    return new XMLHttpRequest();
    }
    else {
    throw new Error('XMLHttp object could not be created!');
    }

    }

    function sendPost() {

    var title = document.getElementById('title').value;
    var post = document.getElementById('post').value;
    var profile = document.getElementById('profile').value;
    var datetime = document.getElementById('datetime').value;
    var url = "?title=" + title + "&post=" + post + "&profile=" + profile +"&datetime=" + datetime;
    var XHR = null;

    XHR.createXMLHttp();
    XHR.open("POST", "/cgi-bin/addpost.cgi", true);
    XHR.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    XHR.onreadystatechange = function() {
    if (XHR.readyState == 4) {
    if (XHR.status == 200) {
    alert("Operazione effettuata con successo");
    }
    else {
    alert("Operazione non riuscita");
    }
    }
    }
    XHR.send(url);

    return false;

    }
    Where is the problem?

  • #4
    Regular Coder
    Join Date
    Jan 2006
    Posts
    568
    Thanks
    6
    Thanked 84 Times in 84 Posts
    XHR.createXMLHttp is undefined. I think it should be XHR=createXMLHttp(); (line 21)

  • #5
    New Coder
    Join Date
    Jan 2010
    Posts
    79
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok, now it works.
    I have other two questions:
    1) I modified sendPost() function, it works but return "NOT OK" message with alert, where is the problem?
    function sendPost() {

    var title = document.getElementById('title').value;
    var post = document.getElementById('post').value;
    var profile = document.getElementById('profile').value;
    var datetime = document.getElementById('datetime').value;
    var url = "?title=" + title + "&post=" + post + "&profile=" + profile +"&datetime=" + datetime;
    var XHR = null;

    XHR = createXMLHttp();
    XHR.open("POST", "/cgi-bin/addpost.cgi", true);
    XHR.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    XHR.onreadystatechange = function() {
    if (XHR.readyState == 4) {
    if (XHR.status == 200 || XHR.status == 304) {
    alert("OK");
    }
    else {
    alert("NOT OK");
    }
    }
    }
    XHR.send(url);

    }
    XHR.status is 0

    2) what I return server side? I must build all web page or only a piece of hit?
    Last edited by dark0s; 09-04-2010 at 12:24 PM.

  • #6
    Regular Coder
    Join Date
    Jan 2006
    Posts
    568
    Thanks
    6
    Thanked 84 Times in 84 Posts
    Not sure about 1. Maybe try it on a few different pages/sites to determine whether it's server- or client side.

    2: You would usually return a string of data or a snippet of HTML to be added to the current DOM; it works like this:

    Code:
    if (XHR.status == 200 || XHR.status == 304) {
    //either:
    var data=XHR.responseText; //data contains whatever string the server sent back
    
    //or:
    var xml=XHR.responseXML; //xml is an XML document object which can be manipulated with standard DOM methods
    
    alert("OK");
    }


  •  

    Posting Permissions

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