Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法从Firefox和Chrome中的PHP页面获取XML响应_Php_Ajax_Html_Xmlhttprequest_Cross Domain - Fatal编程技术网

无法从Firefox和Chrome中的PHP页面获取XML响应

无法从Firefox和Chrome中的PHP页面获取XML响应,php,ajax,html,xmlhttprequest,cross-domain,Php,Ajax,Html,Xmlhttprequest,Cross Domain,我的电脑上有一个本地apache 2.2服务器。我在我的系统上使用PHP5和MySQL 5.0。我在MySQL数据库中创建了一个名为html5的表,该表有一列和一行,其中包含任意字符串 我创建了一个名为sample.PHP的PHP页面,该页面连接到我的MySQL数据库,提取上表的信息,并将其作为XML文件返回 <?php if(!$dbconnect = mysql_connect('localhost', 'root', 'password')) { echo

我的电脑上有一个本地apache 2.2服务器。我在我的系统上使用PHP5和MySQL 5.0。我在MySQL数据库中创建了一个名为html5的表,该表有一列和一行,其中包含任意字符串

我创建了一个名为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;
?>
在Firefox中,catch块返回“Failure”,在Chrome中,警报显示XMLHttpRequest异常101

附言:-

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

  • 在ajax调用中,设置async=“true”或“false”没有任何区别

  • 这是IE9中的回应:-

    萤火虫:-


    谁能给我一个解决方案?我急需一个。

    在php文件中放置以下标题:

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

    更改数据类型并重试

    它只支持:
    dataType(默认值:智能猜测(xml、json、脚本或html))


    参考:

    经过一番努力,我找到了这个解决方案:

    添加标题('Access-Control-Allow-Origin:')很好地解决了我的问题

    谢谢大家的努力


    礼节:

    你能在firebug/chrome inspector的控制台中发布ajax请求的响应吗?Karan,我得到的响应为200 OK。也许可以看看跨源资源共享->只需在firebug控制台中进行检查,你就可以看到响应数据,即使它无法发出警报。Prasanth,更改数据类型根本不会显示任何警报。在IE9中,它会生成一个错误“对象不支持属性或方法”。在Firebug中,响应为200 OK,但无论哪种方式,它都表示XML解析错误。
    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);
    }
    
    header( 'Content-Type: text/xml, charset=utf-8' ); 
    
    $.ajax(
                    {
                        url:"http://localhost/sample.php"+"?randVar="+Math.random(),
                        type: "GET",
                        dataType: "xml",
                        success:function(result)
                        {
                            alert(result);
                        }
                    });