2

I am having the hardest time parsing the response from an ajax request correctly. I have an ajax request that gets back a json array, all I need to do is parse it correctly so I get my two values out.

On the php side, I create the array like so:

while($row = $result->fetch_assoc()) {

            $response[]=array("value" => $row["uid"], "text" => $row["value"]);
        }

My array in PHP looks like this when I print_r:

Array ( [0] => Array ( [value] => 6 [text] => 40 ) [1] => Array ( [value] => 7 [text] => 48 ) )

I then json_encode that array and send it back via ajax. Here is my ajax call:

 $.ajax({
                type: 'GET',
                url: 'ajax.php?s='+selection,
                dataType: 'json',                        
                success: function (data) {
                        $.each( data, function( intValue, currentElement ) {
                                $.each(currentElement, function( key, value ) {
                                    console.log(key);
                                    console.log(value);
                                });
                        });

                }
        });

I would have thought console.log(key) would show 6 and console.log(value) would show 40 but instead I get:

value
6
text
40
value
7
text
48

I really need help just getting 6 and 40 then 7 and 48 (based on array provided).

I have tried a bunch of ways, nothing seems to work.

4 Answers 4

1

You can do something like this using single $.each() , and object property can access like object.property or object\["property"\]

var data = [{
  value: 6,
  text: 40
}, {
  value: 7,
  text: 48
}];
$.each(data, function(i, v) {
  document.write(v.value);
  document.write(v.text);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

2 Comments

I added to my question how I create my array, am I creating it wrong?
@Jason : I couldn't see anything wrong in that. You can use single $.each to iterate
0

This works for me:

 $.ajax({
                    url: URL,
                    dataType: "text",
                    async: true,
                    cache: false,
                    contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                    crossDomain: true,
                    success: function (data2) {

                        json_obj2 = $.parseJSON(data2);

Comments

0

You have an extra each loop that isn't needed

Your javascript data looks like:

[{"value" : 6, "text" : 40}, {"value" : 7, "text" : 48}]

So in first each loop intVal will be index of array and currentElement will be each object instance in the array

The object instance looks like:

{"value" : 6, "text" : 40}

So currentElement.value and currentElement.text are what you are looking for

$.each( data, function( intValue, currentElement ) {
      console.log(currentElement.value);
});

Comments

0

Perhaps, change the php side to be:

$response[$row["uid"]] = $row["value"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.