大家好我是ajax的新手,我正在尝试从php代码中获取数据这是我的ajax代码:
function blodvotingview(contentid)
{
var xmlhttp;
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Your browser does not support XMLHTTP!");
return;
}
var url="index.php";
url=url+"?hp=1";
url=url+"&m=blogenvoting";
url=url+"&contentid="+contentid;
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
这是我的html代码{entry_id}是数字参数:
<a href="" onclick="blodvotingview({entry_id});return false;" title="Vote Up">view</a>
<p>Suggestions: <span id="txtHint"></span></p>
这是我想回复的PHP代码:
<?php
showcomment()
function showcomment()
{
echo "yes";
}
?>
但它不起作用,请帮助我。
答案 0 :(得分:1)
如果您考虑使用jQuery和jquery.serialize插件,您可以通过此示例轻松完成此操作:
$.post('URL', $('#form_id').serialize(), function(r) {
console.log(r);
});
或
$.post('URL', $('#form_id').serialize(), function(r) {
console.log(r);
},'json'); // to parse response as JSON
或
$.get('URL', function(r) {
console.log(r);
},'json'); // to parse response as JSON
答案 1 :(得分:0)
试试这个
function blodvotingview(contentid)
{
var xmlhttp;
if (str==""){
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}else{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","index.php?hp=1q="+str,true);
xmlhttp.send();
}
应该正常工作, Wezy