【问题标题】:How to work with array returned from PHP script using AJAX?如何使用 AJAX 处理从 PHP 脚本返回的数组?
【发布时间】:2013-11-19 20:17:38
【问题描述】:

在过去的几个小时里,我遇到了这个问题,我希望我能在这里得到解决方案。 我想做的是这样的:

PHP:

$errorIds = array();
if(error hapens){
  array_push($errorIds, $user['userId']);
}

$data['results'] = "success";
$data['message'] = "Your message have been sent successfully";
$data['error_id_array'] = $errorIds;
echo json_encode($data);

和 JS:

$.ajax({
               type: "POST",
               url: HTTPS + '/lib/model/data/ctlSms.php?send=1',
               data: $("#smsSendForm").serialize(),
               dataType: 'json',
               success: function(data){
                   $("textarea").removeClass("valid");
                   if(data['results']=="success"){
                       $('#smsSendForm')[0].reset();
                       $('.jtable').find("tbody tr").each(function(){
                            var firstCol = parseInt($(this).find("td:first").text());
                            var inArray = $.inArray(firstCol, data['error_id_array']);
                            if(inArray == -1){
                                $(this).css("background", "green");
                            } else {
                                $(this).css("background", "red");
                            }
                        });
                   } else {
                       console.log(data['message']);
                       $('.jtable').find("tbody tr").each(function(){
                            var firstCol = parseInt($(this).find("td:first").text());
                            var inArray = $.inArray(firstCol, data['error_id_array']);
                            if(firstCol == data['error_id']){
                                $(this).css("background", "red");
                                return false;
                            } else {
                                if(inArray == -1){
                                    $(this).css("background", "green");
                                } else {
                                    $(this).css("background", "red");
                                }
                            }
                        });
                   }
               }
            });

我想要完成的是那个数组中有一个错误,jQuery 检查用户 ID 数组中是否是一个匹配 jTables ID 的 ID,如果匹配比颜色背景红色,否则颜色背景绿色。问题就在这里:

 $('.jtable').find("tbody tr").each(function(){
     var firstCol = parseInt($(this).find("td:first").text());
     var inArray = $.inArray(firstCol, data['error_id_array']); /// here
     if(inArray == -1){
        $(this).css("background", "green");
     } else {
        $(this).css("background", "red");
     }
 });

即使我故意创建错误,我也不会得到正匹配。一切都是绿色的,后端代码效果很好,但是这个前端在过去的几个小时里让我很沮丧。也许我做错了什么???

编辑:

这是 console.log(data['error_id_array']) 给我的反馈:

["2"] 

EDIT2:

好吧,我会从其他角度问。我如何以这样的形式访问 JSON 对象 - console.log 从数据中输出:

Object {results: "success", message: "Your message have been sent successfully",     error_id_array: Array[1]}
error_id_array: Array[1]
   0: "2"
   length: 1
   __proto__: Array[0]
message: "Your message have been sent successfully"
results: "success"
__proto__: Object

我需要检查error_id_array下的数组

【问题讨论】:

  • console.log(data) 给你什么&你确定firstCol 已经被填充了吗?
  • console.log of data['error_id_array'] 给我 ["2"] 如果有一个错误,并且 firstCol 被填充 okey,测试!
  • 为什么不在 PHP 中抛出异常?因此,如果发生错误,您将永远无法在 JS 中访问 success() 函数。
  • 这就是重点,在我的原始代码中有一个 try catch 结构,当没有发送消息时我需要继续,但我需要跟踪失败的消息,然后显示在表格中用红色背景标记它们!

标签: javascript php jquery ajax json


【解决方案1】:

尝试使用 javascript indexOf 而不是 inArray,

console.log(data['error_id_array'].indexOf(firstCol)); // Check whether firstCol has the value

【讨论】:

  • 相同的结果,总是得到-1,这意味着没有找到匹配项。 ://
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多