Reputation: 2783
When result of success function is "null", the message still display "have value".
Can anyone help me, many thanks!
$(document).ready(function () {
setInterval(function () {
$.ajax({
url: "getData.php",
success: function (result) {
if (result.length == 0) {
$("#rlt").append("no value");
} else {
$("#rlt").append("have value");
}
},
dataType: "json"
});
}, 2000);
});
<p id="rlt"></p>
Upvotes: 1
Views: 234
Reputation: 11327
Instead of the value null
, you're probably sending the string "null"
, which will not have a zero length:
Test for the string itself.
if (result === "null") {
$("#rlt").append("no value");
} else {
$("#rlt").append("have value");
}
If you're anticipating an actual null
value in the response, then you wouldn't want to use .length
, because you'll get a TypeError
.
By the way, I don't think a standalone "null"
or null
is valid JSON, but the native parsers don't seem to mind it.
Upvotes: 2