我第一次尝试使用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()参数括号。现在我没有收到错误。但它只是没有做任何事......
答案 0 :(得分:2)
我建议使用现有的库,而不是尝试从头开始运行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
}
});
}
答案 1 :(得分:0)
var xmlhttp;
if (window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest();
else
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
document.getElementById("my").innerHTML=xmlhttp.responseText;
}
xmlhttp.open("POST","page.php,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("firstname=lolo&lastname=koko");