javascript - Ajax , Cannot read property 'documentElement' of null

55

I was watching the New Boston tutorial on Ajax training and I wrote the same code as the tutor of the course did. But the code doesn't work in my computer.

In Google chrome

Uncaught TypeError: Cannot read property 'documentElement' of null
message = xmlHttp.responseXML.documentElement.firstChild.data;

In Firefox

not-well formed bookname.php:5:8
echo '<response>';

index.html

<body onload="process()">
    <h1>Users information</h1>
    Enter user_name you would want to know about:
    <input type="text" id="userInput" />
    <div id="underInput" />
</body>

bookname.php

<?php 
  header('Content-Type: text/xml');
  echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';

  echo '<response>';

  $name = $_GET['userName'];
  $nameArray = array('Mehrshad', 'Alex', 'Tom', 'Aydin');

  if(in_array($name, $nameArray)
  {
      echo 'I do know '. $name . '!';
  }
  else if($name == '')
  {
      echo 'Enter a name you want to know about';
  }
  else
  {
      echo 'Sorry we don\'t have user "'. $name . '" '
  }

  echo '</response>';

?>

bookname.js

var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject()
{
    var xmlHttp;

    if(window.ActiveXObject)
    {
        try {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e) {
            xmlHttp = false;        
        }
    }
    else
    {
        try{
            xmlHttp = new XMLHttpRequest();     
        } catch(e) {
            xmlHttp = false;
        }   
    }

    if(!xmlHttp)
    {
        alert('can\'t create that object hoss!')
    }
    else
    {
        return xmlHttp;
    }

}

function process()
{
    if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
    {
        username = encodeURIComponent(document.getElementById('userInput').value);
        xmlHttp.open("GET", "bookname.php?userName=" + username, true);
        xmlHttp.onreadystatechange = handleServerResponse;
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('process()', 1000);
    }
}

function handleServerResponse()
{
    if(xmlHttp.readyState == 4)
    {
        if(xmlHttp.status == 200)
        {
            message = xmlHttp.responseXML.documentElement.firstChild.data;
            document.getElementById("underInput").innerHTML =
                '<span class="page_speed_605068961"' + message  + '</span>';
                setTimeout('process()', 1000);
        }
        else
        {
            alert('Something went wrong!');
        }
    }

}
208

Answer

Solution:

your php code is invalid because:

  1. you're missing; inecho 'Sorry we don\'t have user "'. $name . '" ';
  2. end parentheses) is missing inif(in_array($name, $nameArray))
  3. > is missing indocument.getElementById("underInput").innerHTML = '<span class="page_speed_605068961">' + message + '</span>';
145

Answer

Solution:

I was also running the same code but it was showing same error as mentioned above. Please suggest the solution because I didn't make the above-mentioned mistakes still it says Uncaught TypeError: Cannot read property 'documentElement' of null at XMLHttpRequest.handleServerResponse.

857

Answer

Solution:

http_request.overrideMimeType('application/xml'); was working for me when IAm fixing between IE to chrome migration

People are also looking for solutions to the problem: php - How to access an array outside if? I use ajax

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.