Getting TypeError: invalid obj operand 'in' when retrieving data using ajax

Below is my ajax call

$(document).ready(function() { $("#blog").focusout(function() { alert('Focus out event call'); alert('hello'); $.ajax({ url: '/homes', method: 'POST', data: 'blog=' + $('#blog').val(), success: function(result) { $.each(result, function(key, val) { $("#result").append('<div><label>' + val.description + '</label></div>'); }); }, error: function() { alert('failure.'); } }); }); }); 

I get "TypeError: invalid" in the error "operand obj" in my console

Thank you in advance

+7
javascript jquery ajax
source share
3 answers

Mention the dataType attribute in your ajax call. It considers the default text. This is why it is not possible to iterate over the result.

 dataType:'json' 

Since your result should be an array or json

+14
source share

the "result" in the success function must be an array

+1
source share

Should data not be an object?

 data: { blog: $('#blog').val() }, 
0
source share