Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

javascript - How to work with array returned from PHP script using AJAX?

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);

and 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");
                                }
                            }
                        });
                   }
               }
            });

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???

EDIT:

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

["2"] 

EDIT2:

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

I need to check array under error_id_array

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try using javascript indexOf instead of inArray,

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...