gpt4 book ai didi

php - 检查 JSON 的响应是否为空

转载 作者:行者123 更新时间:2023-11-30 18:13:12 24 4
gpt4 key购买 nike

我正在通过 JSON 从 MySQL 获取一些数据。但是我想确定响应是否为空,如果它不为空我会显示返回的信息,否则我会显示一条消息。

我尝试了多种可能的解决方案,例如检查 JSON 响应的长度,检查项目严重性是否存在,如果不存在则假设它为 null 等。

这是我的代码片段:

var data = $.ajax({
type: 'GET',
url: 'http://www.mydomain.com/php/loaddata.php?&jsoncallback=?',
dataType: 'JSONp',
timeout: 5000,
success: function(data) {
$.each(data, function(i,item){
if (item.Severity == 1) {
// Do Something
.....

当JSON找到数据时,回复是这样的:更新:

([{"Severity":"1","Latitude":"35.872883","Longitude":"14.449133","Address":"Some Address 1","Heading":"25","Timestamp":"2012-12-28 10:15:03"},{"Severity":"2","Latitude":"35.871269","Longitude":"14.501580","Address":"Some Address 2","Heading":"80","Timestamp":"2012-12-28 10:15:31"}]);

否则如果没有找到数据,JSON 返回:

([]);

最佳答案

因为:Console.log 返回 “string”user1809790 ,
这应该有效:

success: function(data) {
if (data != '' && data !== null) { // Check if the data's not an empty string or null.
$.each(data, function(i,item){
if (item.Severity == 1) {
// Do Something

关于php - 检查 JSON 的响应是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14067580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com