0

I am using Ajax to return my object. I did a console.log(obj) and my object seems perfect. This is how it looks in my console log :

[{"first_name":"jeremiah", "last_name":"test"}, {"first_name":"jay", "last_name":"grens"}]

However, when I come to append each first_name and each last_name to a specific div, I can't make it work. This is how my Ajax looks like :

$('#search_contact').keyup(function() {
var keys = $(this).val();
$.ajax({
    type: 'post',
    url: 'contacts/search',
    data: {'keys':keys},
    success: function(res) {
        console.log(res);
        $('#contacts tbody').html('');
        $.each(res, function(i, val) {
            $('#contacts tbody').append('<tr><td>'+first_name+'</td><td>'+last_name+'</td></tr>');
        });
    }
});
});

When I do this, my #contacts tbody is empty, and I get this error in my console log :

Uncaught TypeError: Cannot use 'in' operator to search for '926' in (my full obj...)

2
  • 2
    Which line is throwing you this error? Commented May 23, 2015 at 20:49
  • Finally found it :) Thank you anyway Terry! Commented May 23, 2015 at 20:53

3 Answers 3

3
$.ajax({
    type: 'post',
    url: 'contacts/search',
    data: {'keys':keys},
    dataType: 'json',
    success: function(res) {
        console.log(res);
        $('#contacts tbody').html('');
        $.each(res, function(i, val) {
            $('#contacts tbody').append('<tr><td>'+val.first_name+'</td><td>'+val.last_name+'</td></tr>');
        });
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

I still get an error :( Uncaught TypeError: Cannot use 'in' operator to search for '926' in
The keys should be a string.
My variable keys is already a string. I get the value of an input with keyup event.
2

To get first_name using $.each, you need to use the 0 index like

$(this)[0]['first_name']

or the val parameter you were passing into the callback function like

val['first_name']

See it working both ways in the snippet

var test = [{"first_name":"jeremiah", "last_name":"test"}, {"first_name":"jay", "last_name":"grens"}];

$.each(test, function(i, val){
       $('body').append('<div>'
                        +$(this)[0]['first_name']+'</div><div>'
                        +val['last_name']+'</div>');
       });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Update

Like others have mentioned, you need to use $.parseJSON to avoid the exception

Uncaught TypeError: Cannot use 'in' operator to search...

or you can set dataType: 'json' to automatically parse the response to JSON

1 Comment

I tried your solution but I still get this error : Uncaught TypeError: Cannot use 'in' operator to search for '926' in
0

Finally I found it, thanks to you guys to point me to the right way.

$('#search_contact').keyup(function() {
var keys = $(this).val();
$.ajax({
    type: 'post',
    url: 'contacts/search',
    data: {'keys':keys},
    success: function(res) {
        console.log(res);
        $('#contacts tbody').html('');
        var res = $.parseJSON(res);
        $.each(res, function(key, val) {
            console.log(val['first_name']);
        });
    }
});
});

As you can see, I added a new line before $.each to convert a string thats come from php script to a json array.

2 Comments

You don't need to parse json if you send a json object.
MathieuBoisjoli, @RomanC is right, just use the ajax call like in his answerh with dataType:'json' and you wont have to use $.parseJSON

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.