Home  >  Article  >  Backend Development  >  问个简单的ajax有关问题,小弟我都急死了

问个简单的ajax有关问题,小弟我都急死了

WBOY
WBOYOriginal
2016-06-13 11:21:18922browse

问个简单的ajax问题,我都急死了~
刚学ajax,写了一段~~感觉js里面的那个if(str=="good")不起作用~~如果单独拿出来返回的话 确实输出good
放进去判断就不行了 不知道为什么啊 求解~弄了我好长时间~~急死了 新手求教!!!!





这是html

<br /><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /><head><br /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><br /><title>无标题文档</title><br /><script type="text/javascript" language="javascript" src="worinima.js"></script><br /></head><br /><br /><body><br /><br /><br /><a href="#" onclick="upsdowns('l','f','fsdf','sdf')">dsds</a><br /><br /><br /></body><br /></html><br />



这是js:
<br /><br />var xmlHttp;<br />function upsdowns(ac,id,ud,mk)//,did)<br />{<br /><br />//获取xmlHttpObject对象,如果为空,提示浏览器不支持ajax<br />  xmlHttp=GetXmlHttpObject();<br />var url;<br /> url="ajax.php"+"?ac="+escape(ac)+"&url="+url+"&id="+escape(id)+"&ud="+escape(ud)+"&mk="+escape(mk)+"&sid="+Math.random();<br /> //回调函数,执行动作<br />xmlHttp.onreadystatechange=stateChanged; <br /> //open<br />xmlHttp.open("GET",url,true);<br />xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");<br />xmlHttp.send(null);<br />} <br /><br />function stateChanged() <br />{<br />	<br />if (xmlHttp.readyState==4)<br />{ <br />  {if(xmlHttp.status==200)   // phparray=new Array()<br />     <br />	 var str=xmlHttp.responseText;<br />	 if(str=="dasda")<br />	 alert(str);<br />	 //document.getElementById("txtHint").innerHTML=str;<br />    <br />   }<br />}<br /><br />}<br />//获取xml对象<br />function GetXmlHttpObject()<br />{<br />var xmlHttp=null;<br />try<br />{<br />// Firefox, Opera 8.0+, Safari<br />xmlHttp=new XMLHttpRequest();<br />}<br />catch (e)<br />{<br />// Internet Explorer<br />try<br /> {<br /> xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");<br /> }<br />catch (e)<br /> {<br /> xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");<br /> }<br />}<br />return xmlHttp;<br />}<br /><br />


这是ajax.php
<br /><br />header("Content-type: text/html;charset=utf-8");<br />header('Vary: Accept-Language'); <br />$w=$_GET['ac'];<br />$a=$_GET['id'];<br />$r=$_GET['ud'];;<br />$t=$_GET['mk'];<br />if($w!==''&&$a!=''&&$r!==''&&$t!=='')<br />{echo"good";<br />}<br /><br />


------解决方案--------------------
function stateChanged() 
{
     
if (xmlHttp.readyState==4)

  {if(xmlHttp.status==200)   // phparray=new Array()
      
     var str=xmlHttp.responseText;
     if(str=="dasda")
     alert(str);
     //document.getElementById("txtHint").innerHTML=str;
     
   }
}
 
}
标红处大括号位置放错了吧,应该是
function stateChanged() 
{
     
if (xmlHttp.readyState==4)

  if(xmlHttp.status==200)   // phparray=new Array()
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn