Php 需要帮助捕获AJAX响应吗
我在我的Php 需要帮助捕获AJAX响应吗,php,ajax,Php,Ajax,我在我的home.php中编写了以下代码。单击提交按钮调用login()函数 function login() { try{ xmlHttp=new XMLHttpRequest(); }catch(e){ try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e2){ try{ xmlHttp=new ActiveXObject("Ms
home.php
中编写了以下代码。单击提交按钮调用login()
函数
function login()
{
try{
xmlHttp=new XMLHttpRequest();
}catch(e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}catch(e2){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch(e3){
alert('Sorry ! AJAX not supported');
}
}
}
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4&&xmlHttp.status==200){
alert(xmlHttp.responseText);
}
}
xmlHttp.open('POST','ajax_call.php',true);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
xmlHttp.send(document.getElementById('username').value,document.getElementById('password').value);
}
</script>
并按顺序得到了跟踪
1 0
2 200
3 200
4 200
hello
1 0
hello
login()
{
试一试{
xmlHttp=新的XMLHttpRequest();
}捕获(e){
试一试{
xmlHttp=新的ActiveXObject(“Microsoft.xmlHttp”);
}渔获物(e2){
试一试{
xmlHttp=新的ActiveXObject(“Msxml2.xmlHttp”);
}渔获物(e3){
警报(“抱歉!不支持AJAX”);或
}
}
}
xmlHttp.onreadystatechange=函数(){
if(xmlHttp.readyState==4&&xmlHttp.status==200){
警报(xmlHttp.responseText);
}
}
open('POST','ajax_call.php',true);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);/*Anil-Remove*/
发送('username'.value,'password'.value);/*尝试静态用户名和密码*/
}
[ajax call][1]
[1]: http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_post2
最后一行应该是
xmlHttp.send();
但看起来你想做一些身份验证。。。如果是基本身份验证,那么值应该放在头中,或者如果是服务器身份验证,则取决于您的实现。。。您能解释一下为什么在xmlHttp.send()调用中使用用户名/密码吗?只是一个想法:
- 我认为在更改ReadyState之前,您必须打开您的xmlHttp李>
- 您是否尝试过此。responseText
1 0
2 200
3 200
4 200
hello
1 0
hello
login()
{
try{
xmlHttp=new XMLHttpRequest();
}catch(e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}catch(e2){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch(e3){
alert('Sorry ! AJAX not supported');Or
}
}
}
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4&&xmlHttp.status==200){
alert(xmlHttp.responseText);
}
}
xmlHttp.open('POST','ajax_call.php',true);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");/* Anil Remove ;*/
xmlHttp.send('username'.value,'password'.value);/* try static username and password*/
}
</script>[ajax call][1]
[1]: http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_post2
xmlHttp.send();