XMLHttpRequest Problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: XMLHttpRequest Problem

Hybrid View

  1. #1
    Join Date
    May 2010
    Posts
    2

    XMLHttpRequest Problem

    Hey,
    I have quite a bit of experience with PHP but this is my first time ever using XMLHttpRequest. I virtually copied some code from my book, but I'm getting a status of 0. I have crawled the internet to attempt and find out what this means, but none of the cases in which it is used seems applicable. Below, I have my code. PHP and Ajax both report no errors, and file fileInteract.php works in isolation. pushMenu correctly generates an XMLHttpRequest, defines variable page correctly, and enters the onreadystatechange function before being stopped by the if. Any help would be fantastic. Thank you in advance.

    FYI, I am trying to load a dynamically created select based on what the user clicks in another select as you can probably tell.


    //part of home.php, the caller of pushMenu and the XMLHttpRequest
    PHP Code:
    <?php
      $l 
    mysql_connect("mysql""(undisclosed)""(undisclosed)") or die('Error, could not connect');
      
    mysql_select_db("mainpointer");

      
    $q "SELECT * FROM Supercatagories ORDER BY name";
      
    $r mysql_query($q);
      echo 
    '<select name = "findOne" onchange = "pushMenu(this.options[this.selectedIndex].value)">';
      echo 
    '<option value = "" disabled = "disabled">Choose a category</option>';
      while (
    $row mysql_fetch_array($r)) {
        echo 
    "<option value = ".$row['point'].">".$row['name']."</option>";
      }
      echo 
    '</select>';
    ?>

    //functions.js, loads correctly. readystate = 4, status = 0. menuChange is a span further along in the code in home.php

    Code:
    function ajaxRequest() {
        var request = false;
        try {
          request = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
          try {
            request = new ActiveXObject("Microsoft.XMLHTTP");
          }
          catch (E) {
            request = new XMLHttpRequest();
          }
        }
        return request;
      }
      function pushMenu(id) {
        var xmlhttp = new ajaxRequest();
        var page = 'fileInteract.php?name='+id;
        xmlhttp.open("GET", page); 
        xmlhttp.onreadystatechange = function() {
          if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById('menuChange').innerHTML = xmlhttp.responseText;
          }
        }
        xmlhttp.send();
      }

    //file interact.php, probably not a problem because it works in isolation with the page i call.
    PHP Code:
    <?php
      
    include_once "dbconnect.php";
      
    $name $_GET['name'];

      
    mysql_select_db($name) or DIE("Problem with connection");
      
      
    $q "SELECT * FROM Pointer ORDER BY Name";
      
    $r mysql_query($q) or DIE("Problem with query");

      echo 
    '<select name = findTwo>';
      echo 
    '<option value = "" disabled = "disabled" onchange = "document.finder.submit();">Choose a category</option>';
      while (
    $row mysql_fetch_array($r)) {
        echo 
    '<option value = "'.$row['point'].'" onclick = "submit()">'.$row['name'].'</option>';
      }
      echo 
    '</select>';
    ?>
    Once again, no reported errors, pages work fantastically except for the xmlhttprequest. If I had a suspicion, the problem has to do with home.php because the other code is almost exactly copy pasted from a book, but I do not know for sure. And yes, I am new to this, so if it is something very stupid then I would not be surprised. Finally, I know certain browsers seem to have random problems with xmlhttprequest, but this is not working in IE8, Opera, Firefox, or Chrome (all Windows)
    Last edited by PeejAvery; May 25th, 2010 at 06:58 AM. Reason: Added php and code tags.

  2. #2
    Join Date
    May 2010
    Posts
    2

    Re: XMLHttpRequest Problem

    Answered my own question: I definitely should have looked farther down in the forum because even though it was on a different topic it lead me to the answer.

    I modified the code I posted so that the page that XMLHttpRequest was opening did not contain my domain name, when the original source did. The original source code exerpt is as follows.

    Code:
    function pushMenu(id) {
        var xmlhttp = new ajaxRequest();
        var page = 'http://www.in2itech.com/fileInteract.php?name='+id;
        xmlhttp.open("GET", page); 
        xmlhttp.onreadystatechange = function() {
          alert(xmlhttp.status);
          if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById('menuChange').innerHTML = xmlhttp.responseText;
          }
        }  
        xmlhttp.send();
      }
    Even though I am requesting the same domain which I am on, somehow the request was treating it as a different domain or something and thus an error was caused. The following only slightly modified code works fine:

    Code:
    function pushMenu(id) {
        var xmlhttp = new ajaxRequest();
        var page = 'fileInteract.php?name='+id;
        xmlhttp.open("GET", page); 
        xmlhttp.onreadystatechange = function() {
          alert(xmlhttp.status);
          if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById('menuChange').innerHTML = xmlhttp.responseText;
          }
        }
        xmlhttp.send();
      }
    I have no idea why this caused a 0 error, any ideas? After all, I was requesting my own domain...
    Last edited by PeejAvery; May 25th, 2010 at 06:59 AM. Reason: Corrected the code tags.

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center