douqiao8370 2013-05-09 01:02
浏览 58

Ajax POST Requests返回int而不是string

I have the following JS code:

var dataString = "action=validateUsername"+
         "&username="+usernameRegi+
         "&lang="+lang;
$.ajax({
type: "POST",
url: "function.php",
data: dataString,
cache: false,
success: function(result)
    {           
        var expl=result.split("|");

                if(expl[0]=="1")
            alert("1");

                else if(expl[0]=="99")
        alert("99");
    }
});

This is my function.php

if($_POST["action"]=="validateUsername")
{
    $username=$_POST["username"];
    $lang=$_POST["lang"];

    $sqlSelect = "SELECT * FROM user where userName='".$username."'";
    $sqlQuery = mysql_query($sqlSelect);
    $rowCount = mysql_num_rows($sqlQuery);

    if($rowCount>0)
    {
    if($lang=="BM")
        echo "99|My msgA.";
    else
        echo "99|My msgB.";
    }
    else
    {
    if($lang=="BM")
        echo "1|My msgC.";
    else
        echo "1|My msgD.";  
    }
}

The problem is, my ajax request never alert 1 or 99 on success. And I found the problem is expl[0]==1 instead of expl[0]=="1".

When I turn my code to this one, it run smooth.:-

if(expl[0]==1)
alert("1");

else if(expl[0]==99)
alert("99");

This is happen only when I upload my code to the server. No problem on the localhost. What is the problem, is there any setting on the server that cause that problem?

Could anybody explain me, what is happening here?

  • 写回答

2条回答 默认 最新

  • doutongya8378 2013-05-09 05:32
    关注

    It's because:

    When you assign a numeric value to a variable, do not put quotes around the value. If you put quotes around a numeric value, it will be treated as text.

    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题