#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    28
    Rep Power
    0

    Controlling XMLHttpRequest Response


    Hi

    Here's what I tried:
    glike.php
    PHP Code:
    //...

    if ($q) {
    echo (
    1);
    } else {
    echo (
    0);

    question.html
    Code:
    javascript Code:
    <script>//...
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
     
      //...  
     
      var answer = xmlhttp.responseText;
     
      if (answer == 1) {
        alert('You like this game');
      } else {
        alert('Oops');
      }
     
    }
     
    </script>
    This works fine but only when I open/send the request using GET. It doesn't work for POST! Any idea why and what could b the solution?

  2. #2
  3. Banned
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,938
    Rep Power
    0
    Hi,

    what do you mean by "it doesn't work"?

    Does the request itself fail? If so, what's the exact error message (on the JavaScript console of your browser)? Are you sure you got the request logic right?

    Or is it a PHP problem? If so, are you sure you're reading from $_POST rather than $_GET?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    28
    Rep Power
    0
    Originally Posted by Jacques1
    Hi,

    what do you mean by "it doesn't work"?

    Does the request itself fail? If so, what's the exact error message (on the JavaScript console of your browser)? Are you sure you got the request logic right?

    Or is it a PHP problem? If so, are you sure you're reading from $_POST rather than $_GET?
    Yes, I use the same code. The only thing I change is the method I use to open/send the request:

    NOK code:
    PHP Code:
    if (isset($_POST['a'])){ 
      
    $a $_POST['a']; 

      if (
    $a == '1'){ 
        echo (
    1); 
      } else { 
        if (
    $a == '0'){ 
         echo (
    0); 
       } else { 
          echo (
    9); 
       } 
      } 
    } else { 
      echo (
    "This is not an answer!"); 

    Code:
    javascript Code:
     
     
    <script>
    function loadXMLDoc()
    {
    var xmlhttp;
    var answer;
     
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        //document.getElementById("a_box").innerHTML=xmlhttp.responseText;
     
        answer = xmlhttp.responseText;
        if (answer == 1){
         alert('You like the game!!');
        }
     
        if (answer == 0){
         alert('You dont like the game!!');
        }
     
        if (answer == 9){
         alert('What??!!');
        }
     
        } else {
          document.getElementById("debugDiv").innerHTML = xmlhttp.readyState+ " ... " + xmlhttp.status;
        }
      }
     
    var a = document.getElementById("a").value;
     
    xmlhttp.open("POST","glike.php?a="+a,true);
    xmlhttp.send();
    }
    </script>
    Everything will work fine if you decide to do GET!


    NOTE: in both methods get/post. If I remove this line:
    Code:
    document.getElementById("a_box").innerHTML=xmlhttp.responseText;
    The readyState is always 3!! Yet my alert messages work when I use GET! Strange because if it's 3, the 4 never get true and 4 comes always after 3 (process).
  6. #4
  7. Banned
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,938
    Rep Power
    0
    You do not use the right request code. Please check the link I gave you. A POST request is very different from a GET request, you cannot just change xmlhttp.open("GET" ...) to xmlhttp.open("POST" ...).

    Also consider using a framework like jQuery to get rid of lowlevel HTTP stuff.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    28
    Rep Power
    0
    Code:
     xmlhttp.open("POST","a.php",true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send("a="+a);
    This works fine, I forgot the "+" in the source code.
    Other headers with content length and connection don't seem to be OK.

    Now, why does xmlhttp.readyState always return 3 when 4 is true?

    Thx

IMN logo majestic logo threadwatch logo seochat tools logo