如何使用AJAX从PHP脚本返回的数组?

时间:2013-11-19 20:17:38

标签: javascript php jquery ajax json

过去几个小时我已经堆积了这个问题,我希望我能在这里找到解决方案。 我想做的是这样的事情:

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,如果匹配比collors背景为red,则其他颜色为background green。问题出在这里:

 $('.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 out from data:

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

下的数组

1 个答案:

答案 0 :(得分:0)

尝试使用javascript indexOf而不是inArray,

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