Javascript输出运行异常

Javascript输出运行异常,javascript,jquery,json,Javascript,Jquery,Json,我有一个 <p id="err_output"></p> 其功能如下: function check_username_existence(){ $.ajax({ url: './php/user_name_availability.php', data: { username : $('#username').val() }, type: 'post', success: function(

我有一个

        <p id="err_output"></p>
其功能如下:

function check_username_existence(){
    $.ajax({ url: './php/user_name_availability.php',
         data: { username : $('#username').val() },
         type: 'post',
         success: function(output) {
                var json = $.parseJSON(output);
                $('#err_output').html(json.response.exist);

                if(json.response.exist == 'true'){
                //  $('#err_output').html('Exists');
                }
         }
    });
};
json响应的值为:

{ "response" : { "exist" : true   } }
{ "response" : { "exist" : false  } }
问题是它只在exist为真时才输出

如果我把

 $('#err_output').html( output + json.response.exist);
另一方面,它也会输出假值。

这一行

if(json.response.exist == 'true'){
与字符串
“true”
进行比较,但您存储了布尔值
true
,它应可用于:

if (json.response.exist) {

丢失引号并使用标识运算符(==)。它会给你你期望的结果

if(json.response.exist === true){
弱比较会给你带来奇怪的结果。下面是一个例子,说明了为什么它会以代码中的方式进行评估

bool = "false";
if(bool){
  // bool evaluates to true because it is defined as a string
}

bool = 'true';
if(bool == true){
  // doesn't execute. comparing string to boolean yields false
}

非常感谢。我想我被php的自动播放给宠坏了。
bool = "false";
if(bool){
  // bool evaluates to true because it is defined as a string
}

bool = 'true';
if(bool == true){
  // doesn't execute. comparing string to boolean yields false
}