php调用ajax时报undefined错误,代码如下.
ajax.js:
var xmlHttp;
//xmlhttp对象初始化
function createXMLHttp()
{
if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
alert(e);
}
}
}
}function ajaxService(url)
{
createXMLHttp();
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange = callback;
xmlHttp.send(null);
}function callback()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
var result = result = xmlHttp.responseText;
return result;
}
}
}test.html:
<script type="text/javascript">
function startService()
{
var text = document.getElementById("text").value;
var ddd = ajaxService("service.php");
alert(ddd);-----此处弹出的提示框的信息是:undefined(请问怎么回事呢)
}
</script>
<input type="text" id="text"/>
<input type="button" onclick="startService()" value="test"/>service.php:
<?
echo "1";
?>
ajax.js:
var xmlHttp;
//xmlhttp对象初始化
function createXMLHttp()
{
if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
alert(e);
}
}
}
}function ajaxService(url)
{
createXMLHttp();
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange = callback;
xmlHttp.send(null);
}function callback()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
var result = result = xmlHttp.responseText;
return result;
}
}
}test.html:
<script type="text/javascript">
function startService()
{
var text = document.getElementById("text").value;
var ddd = ajaxService("service.php");
alert(ddd);-----此处弹出的提示框的信息是:undefined(请问怎么回事呢)
}
</script>
<input type="text" id="text"/>
<input type="button" onclick="startService()" value="test"/>service.php:
<?
echo "1";
?>
var ddd = callback(); alert(ddd);
if(xmlHttp.status == 200)
{
result = xmlHttp.responseText;
//return result;
}
....function startService()
{
var text = document.getElementById("text").value;
ajaxService("service.php");
alert(result);
}
callback 是 xmlHttp 的回调函数,其返回值是返回给 xmlHttp 的
并不能通过 ajaxService 返回
{
var text = document.getElementById("text").value;
var ddd = ajaxService("service.php");
alert(ddd);-----此处弹出的提示框的信息是:undefined(请问怎么回事呢)
}
你这个函数有什么作用啊,你可以把这个东西写到
function callback()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
var result = result = xmlHttp.responseText;
alert(result); // 我一般是这样写的document.getElementById("getshowct").innerHTML=retext;
}
}
}