当前位置: 代码迷 >> Ajax >> 高手帮小弟我找出异常
  详细解决方案

高手帮小弟我找出异常

热度:311   发布时间:2012-03-26 15:46:56.0
高手帮我找出错误
四个文件,计算除法得出结果。
morephp.html
HTML code
<!DOCTYPE html>
<html>
  <head>
    <title>Practical AJAX: PHP Parameters and Error Handling</title>
    <script type="text/javascript" src="morephp.js"></script>
  </head>
  <body>
  Ask server to divide
  <input type="text" id ="firstNumber" />
  by
  <input type="text" id ="secondNumber" />
  <input type="button" value="Send" onclick="process()" />
  <div id="myDivElement" />

  </body>
</html>


morephp.js
JScript code
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject()
{
  var xmlHttp;
  try
  {
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP"
                                    "Microsoft.XMLHTTP");

   for ( var i=0;i<XmlHttpVersions.length && !xmlHttp;i++)
   {
      try
      {
        xmlHttp = new ActiveXobject(XmlHttpVersions[i]);
      }
      catch(e) {}
   }                                                           
  }
if (!xmlHttp)
  alert("Error creating the XMLHttpRequest object.");
else
  return xmlHttp;
}


function process()
{

  if (xmlHttp)
  {
     try
     {
       var firstNumber = document.getElementById("firstNumber").value;
       var secondNumber = document.getElementById("secondNumber").value;

       var params = "firstNumber=" + firstNumber +
                    "&secondNumber=" + secondNumber;
       xmlHttp.open("GET","morephp.php?" =params, true);
       xmlHttp.onreadystatechange = handleRequestStateChange;
       xmlHttp.send(null);

     }
     catch (e)
     {
       alert("Can't connect to server:\n" + e.toString());    
     }
  }
}

function handleRequestStateChange()
{
   if (xmlHttp.readyState == 4)
   {
      if (xmlHttp.readyState == 200)
      {
        try
        {
          handleServerResponse();
        } 
        catch(e)
        {
          alert("Error reading the response:" + e.toString());
        }
      }
      else
      {
        alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
      }
   }
}


function handleServerResponse()
{
  var xmlResponse = xmlHttp.responseXML;
  if (!xmlResponse || !xmlResponse.documentElement)
     throw("Invalid XML structure:\n" + xmlHttp.responseText);
  var rootNodeName = xmlResponse.documentElement.nodeName;
  if (rootNodeName == "parsererror")
    throw("Invalid XML structrue:\n" +xmlHttp.responseText);
  xmlRoot = xmlResponse.documentElement;
  if (rootNodeName != "response" || !xmlRoot.firstChild)
    throw("Invalid XML structure:\n" + xmlHttp.responseText);
  responseText = xmlRoot.firstChild.data;
  myDiv = document.getElementById("myDivElement");
  myDiv.innerHTML = "Server Says the answer is : " +responseText;
}


morephp.php
PHP code
<?php
require_once('error_handler.php');
header('Content-Type: text/xml');
$firstNumber = $_GET['firstNumber'];
$secondNumber = $_GET['secondNumber'];
$result = $firstNumber / $secondNumber;
$dom = new DOMDocument();
$response = $dom->createElement('response');
$dom->appendChild($response);
$responseText = $dom->createTextNode($result);
$response->appendChild($responseText);
$xmlString = $dom->saveXML();
echo $xmlString;
?>
 
  相关解决方案