php - AJAX 返回 null XML

标签 php javascript xml ajax

我是第一次尝试使用 AJAX,但我很难掌握它的窍门。这是请求代码:

function requestData(j) {
    var xmlhttp;
        if (window.XMLHttpRequest) {
            xmlhttp=new XMLHttpRequest();
        }
        xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                    xml = xmlhttp.responseXML;
                    post = "";
                    title = "";
                    postdata = xml.getElementsByTagName("post");
                    titledata = xml.getElementsByTagName("title");
                    datedata = xml.getElementsByTagName("date");
                    timedata = xml.getElementsByTagName("time");
                    document.getElementById("post").value = postdata[0].childNodes[0].nodeValue;
                    document.getElementById("heading").value = titledata[0].childNodes[0].nodeValue;
                    document.getElementById("date").value = datedata[0].childNodes[0].nodeValue;
                    document.getElementById("time").value = timedata[0].childNodes[0].nodeValue;
                    document.getElementById("id").value = j;
                    document.getElementById("update").value = "true";
                }
            };
        xmlhttp.open("POST","../script/getnewsdata.php",true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.send("ID=" + j);
        return false;
    }

Firebug 告诉我 postdata = xml.getElementsByTagName("post"); 行上的“xml 为 null”;这意味着 xmlhttp.responseXML 为 null。

这是服务器端脚本:

<?php
$db = mysql_connect("wadafw","awfawf","awfsgv");
if(!$db)
{
  die("Could not connect: " . mysql_error());
}
mysql_select_db("afggbare", $db);
$updata = mysql_query('SELECT * FROM News WHERE NewsID='.$_POST['ID']);
$blog = mysql_fetch_array($updata);
$post = $blog['Content'];
$regex = Array('/<br />/', '/<(\/?)(b|i|u)>/', '/<a href="(http://[www.]?\w+)">(\w+)<\/a>/', '/<div class="media"><iframe title="YouTube video player" width="425" height="265" src="http://www.youtube.com/embed/(\w+)hd=1" frameborder="0" allowfullscreen></iframe></div>/', '/<div class="media"><img width="425" src="(http://[www.]?[\w+])" /></div>/');
$regReplace = Array('\r\n', '[$1$2]', '[link=$1]$2[/link]', '[youtube]http://www.youtube.com/watch?v=$1[/youtube]',
                '[img]$1[/img]');
$post = preg_replace($regex, $regReplace, $post);

echo    '<newsItem>
        <title>'.$blog['Heading'].'</title>
        <post>'.$post.'</post>
        <date>'.$blog['time'].'</date>
        <time>'.$blog['time'].'</time>
    </newsItem>';
?>

正则表达式可能很糟糕......但现在这并不重要。

更改为这个...现在收到“函数未定义”错误:

function requestData(j) {
        $.ajax("../script/getnewsdata.php", {
            data: {ID: j},
            type: "POST",
            dataType: "xml",
            success: function(data, status, jqXHR){
                var xml = jqXHR.responseXML;
                postdata = xml.getElementsByTagName("post");
                titledata = xml.getElementsByTagName("title");
                datedata = xml.getElementsByTagName("date");
                timedata = xml.getElementsByTagName("time");
                document.getElementById("post").value = postdata[0].childNodes[0].nodeValue;
                document.getElementById("heading").value = titledata[0].childNodes[0].nodeValue;
                document.getElementById("date").value = datedata[0].childNodes[0].nodeValue;
                document.getElementById("time").value = timedata[0].childNodes[0].nodeValue;
                document.getElementById("id").value = j;
                document.getElementById("update").value = "true";
            }


        }
    }

好的,发现问题了。这只是一个拼写错误...没有关闭 $.ajax() 参数括号。现在我没有收到错误。但它只是没有做任何事情......

最佳答案

我建议使用现有的库,而不是尝试从头开始运行 AJAX。 jQuery 使这项任务变得极其简单。

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

function requestData(j) {
    $.ajax('../script/getnewsdata.php', {
        data: {ID: j},
        type: 'POST',
        dataType: 'xml',
        success: function(data, status, jqXHR){
            // consume data here
        }
    });
}

关于php - AJAX 返回 null XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9525746/

相关文章:

php - 如何在 codeigniter 中提交表单后解决 "302 found"响应

PHP/ curl : inspecting response headers before downloading body

javascript - jQuery 搜索过滤器显示列表项标题

javascript - UI设计: templates for wizard driven app?

javascript - 谷歌脚本: XML parsing errors

php - 将 Zend 框架中的默认上下文设置为 XML

php - 目录内容更改时刷新页面

javascript - Charts.js 图不缩放到 Canvas 大小

c# - XML 的 Saml2Assertion 为空

xml - 在哪里向 XSD 架构添加版本?