2013-01-22 51 views
0

我的电脑上有一个本地apache 2.2服务器。我在我的系统上使用PHP 5和MySQL 5.0。我在我的MySQL数据库中创建了一个名为html5的表,它有一列和一行包含任意字符串。无法在Firefox和Chrome中从PHP页面获取XML响应

我创建了一个名为sample.php的PHP页面,它连接到我的MySQL数据库并提取上表的信息并将其作为XML文件返回。

<?php 
    if(!$dbconnect = mysql_connect('localhost', 'root', 'password')) { 
     echo "Connection failed to the host 'localhost'."; 
     exit; 
    } // if 
    if (!mysql_select_db('mysampledb')) { 
     echo "Cannot connect to database 'mysampledb'"; 
     exit; 
    } // if 

    $table_id = 'html5'; 
    $query = "SELECT * FROM $table_id"; 
    $dbresult = mysql_query($query, $dbconnect); 

    // create a new XML document 
    $doc = new DomDocument('1.0'); 

    // create root node 
    $root = $doc->createElement('note'); 
    $root = $doc->appendChild($root); 

    // process one row at a time 
    while($row = mysql_fetch_assoc($dbresult)) 
    { 
     // add node for each row 
     $occ = $doc->createElement($table_id); 
     $occ = $root->appendChild($occ); 

     // add a child node for each field 
     foreach ($row as $fieldname => $fieldvalue) 
     { 
      //create child element in xml file 
      $child = $doc->createElement($fieldname); 
      $child = $occ->appendChild($child); 

      //add database content into the child element created above 
      $value = $doc->createTextNode($fieldvalue); 
      $value = $child->appendChild($value); 

     }// foreach 
    }// while 

    // get completed xml document 
    $xml_string = $doc->saveXML(); 
    echo $xml_string; 
?> 

我曾经在一个单独的目录,使一个简单的请求以上PHP页面按钮的点击,并试图抢响应,并显示为警告创建的HTML5页面。

这是HTML5页面: -

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <script type="text/javascript" src="jquery.js"></script> 
    </head> 
    <body>   
     <button onclick="myFunc()">Click me</button> 
     <script type="text/javascript"> 
      function myFunc() 
      {    
       $.ajax(
        { 
         url:"http://localhost/sample.php"+"?randVar="+Math.random(), 
         type: "GET", 
         dataType: "text/xml", 
         success:function(result) 
         { 
          alert(result); 
         } 
        }); 
      } 
     </script> 
    </body> 
</html> 

但是当我运行此页我得到的只有在IE9的响应。在Firefox和Chrome中,我看到为空警报

在尝试这种方式之前,我也尝试过使用XMLHttpRequest对象,但即使如此,我只能在IE9上获得响应。

function myFunc() 
{ 
    var xmlhttp, xmlDoc; 
    if (window.XMLHttpRequest) 
    { 
     xmlhttp=new XMLHttpRequest(); 
    } 
    else 
    { 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    try 
    {     
     xmlhttp.open("GET","http://localhost/sample.php"+"?randVar="+Math.random(),false); 
     xmlhttp.send(); 
     xmlDoc=xmlhttp.responseText; 
    } 
    catch(e) 
    { 
     alert(e.message); 
    }    
    alert(xmlDoc); 
} 

在Firefox中,catch块返回 “失败”,并在Chrome中,警报说XMLHttpRequest的异常101

PS: -

  1. 保持HTML文件相同的htdocs文件夹的sample.php页面将解决这个问题,但其中服务器是一个独立的计算机上它不可能在现实世界不同的网络。所以我不在寻找这个建议。

  2. 在ajax调用中,使async =“true”或“false”没有任何区别。

这是IE9的响应: - Alert Message

萤火虫: - Firebug

任何人都可以请您提供一个解决方案吗?我需要一个不好的。

+0

你可以张贴在萤火/ Chrome检查的控制台Ajax请求的响应? –

+0

卡兰,我得到的答复200好。 – Kumar

+0

也许看看Cross Origin资源共享 - > http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing – Crisp

回答

0

将在你的PHP文件中的以下标题:

header('Content-Type: text/xml, charset=utf-8'); 
+0

没有帮助Karan .. :-( – Kumar

0

更改数据类型和尝试。

它只支持:dataType (default: Intelligent Guess (xml, json, script, or html))

$.ajax(
       { 
        url:"http://localhost/sample.php"+"?randVar="+Math.random(), 
        type: "GET", 
        dataType: "xml", 
        success:function(result) 
        { 
         alert(result); 
        } 
       }); 

编号:http://api.jquery.com/jQuery.ajax/

+0

只是检查你的萤火虫控制台,你可以看到回应数据在那里,即使它不能警报 –

+0

Prasanth,改变数据类型根本没有显示任何警报,而在IE9中,它产生一个错误“对象不支持属性或方法” – Kumar

+0

在Firebug中,响应是200好,但无论如何它说XML解析错误。 – Kumar