调用php时ajax不起作用

调用php时ajax不起作用,php,jquery,xml,ajax,Php,Jquery,Xml,Ajax,这是我的jquery脚本: <script> $(document).ready(function(){ $("#username").blur(function() { //remove all the class add the messagebox classes and start fading $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slo

这是我的jquery脚本:

  <script>
  $(document).ready(function(){
    $("#username").blur(function()
{
 //remove all the class add the messagebox classes and start fading
 $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
 //check the username exists or not from ajax
 $.post("check.php",{ user_name:$(this).val() } ,function(data)
 {
  if(data=='no') //if username not avaiable
  {
   $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
   {
    //add message and change the class of the box and start fading
    $(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
   });
  }
  else
  {
   $("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
   {
    //add message and change the class of the box and start fading
    $(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);
   });
  }
 });
});
  });
  </script>

$(文档).ready(函数(){
$(“#用户名”).blur(函数()
{
//删除所有类添加messagebox类并开始淡入淡出
$(“#msgbox”).removeClass().addClass('messagebox').text('Checking…').fadeIn(“slow”);
//从ajax检查用户名是否存在
$.post(“check.php”,{user_name:$(this.val()},函数(数据)
{
if(data='no')//如果用户名不可用
{
$(“#msgbox”).fadeTo(200,0.1,function()//开始淡入messagebox
{
//添加消息并更改框的类别,然后开始淡入淡出
$(this.html('此用户名已存在').addClass('messageboxerror').fadeTo(900,1);
});
}
其他的
{
$(“#msgbox”).fadeTo(200,0.1,function()//开始淡入messagebox
{
//添加消息并更改框的类别,然后开始淡入淡出
$(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);
});
}
});
});
});
这是我的php脚本:

<?php
header("Content-Type: text/plain");
$username=$_POST["user_name"]; 
$xml = simplexml_load_file("usernames.xml");
foreach($xml->children() as $child)
  {
  if ( $child == $username ) {
    echo "no";
}
    }
?> 

这是我的xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
 <usernames>

        <num>bobby</num> 
        <num>murali</num> 
        <num>rakesh</num> 
        <num>manoj</num> 


    </usernames> 

警察
穆拉里
拉凯什
马诺伊

我不明白问题出在哪里。我认为php正在被调用并回显正确的语句。但我认为我的脚本没有正确地捕捉到它。我不明白问题存在的地方。无论名称是什么,输入它只是跳过
if
循环并在我的jquery脚本中输入else。我已经单独执行了php,并且它工作正常。我认为问题在于jquery脚本。当我连接到数据库并检查用户名的可用性时,我的脚本工作正常。

尝试:
{'user_name':$(this.val()}

使用firebug,查看发布的内容,并将其与PHP文件正在检查的数据进行比较。另外,还有一个AJAX回调的警报,看看返回了什么我得到正确的响应,但它总是转到“else”循环。即使
循环满足要求。我使用了firebug。一切都是正确的,它返回的数据。在jquery脚本中处理数据时出错。添加
警报(data='no')
,根据您的说法,此代码的计算结果应为false,这意味着
数据
绝对不是
=='no'
。在firebug上使用
console.log(data)
并将结果粘贴到这里。我使用了
alert(data='no')。我总是得到false。我在脚本中使用了
console.log(data)`当我在firebug中看到它时,它给出了正确的信息。如果我输入了“manoj”在xml文件中,当我输入另一个名字时,我得到一个空的名字。我在前面使用了
console.log(data)jst
if(data=='no')`