如何使用AJAX处理从PHP脚本返回的数组? [英] How to work with array returned from PHP script using AJAX?

查看:64
本文介绍了如何使用AJAX处理从PHP脚本返回的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

for past few hours i have stack with this problem, i hope i can get a solution here. What i am trying to do is something like this:

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数组中的ID是否为可匹配jTables ID的ID,以及是否匹配的颜色为coll背景红色,否则为背景绿色.问题出在这里:

What i want to accomplish is that of there is a error in that array, jQuery checks if in array of user IDs is an ID that maches jTables IDs and if matches than collors background red else colors background green. And the problem is somehere here:

 $('.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");
     }
 });

即使我有意造成错误,我也不会得到正向匹配.一切都变成绿色,后端代码很好用,但是在过去的几个小时中,这个前端让我感到沮丧.也许我在找错地方了?

I dont get possitive match even if i intentionaly create an error. Everything is colored green, back end code works great, but this frontend is frustrating me for past few hours. Maybe i am doind something wrong???

这是console.log(data ['error_id_array'])返回给我的东西:

This is what console.log(data['error_id_array']) gives back to me:

["2"] 

我会从其他角度询问.我如何以这种形式访问JSON对象-从数据中放出console.log:

Okey i will ask from other perspective. How do i access JSON object in form like this - console.log out put 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下的数组

I need to check array under error_id_array

推荐答案

尝试使用javascript indexOf代替inArray,

Try using javascript indexOf instead of inArray,

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

这篇关于如何使用AJAX处理从PHP脚本返回的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆