jquery - Iterating JSON list results in a "Error: Syntax error, unrecognized expression" -
i have ajax call, calls controller. controller returns following json:
{"officeproducts":"[{\"id\":96,\"myproperty\":null,\"enabled\":true,\"envelope\":{\"id\":1,\"quality\":\"god\",\"papersize\":\"a4\",\"type\":\"window\"}},{\"id\":169,\"myproperty\":null,\"enabled\":true,\"envelope\":{\"id\":1,\"quality\":\"god\",\"papersize\":\"a4\",\"type\":\"window\"}},{\"id\":174,\"myproperty\":null,\"enabled\":true,\"envelope\":{\"id\":1,\"quality\":\"god\",\"papersize\":\"a4\",\"type\":\"window\"}},{\"id\":175,\"myproperty\":null,\"enabled\":true,\"envelope\":{\"id\":1,\"quality\":\"god\",\"papersize\":\"a4\",\"type\":\"window\"}}]"}
and want iterate list. want iterate officeproducts
.
i have following code, wrong, a:
error:
error: syntax error, unrecognized expression: [{"id":96,"myproperty":null,"enabled":true,"envelope":{"id":1,"quality":"god","papersize":"a4","type":"window"}},{"id":169,"myproperty":null,"enabled":true,"envelope":{"id":1,"quality":"god","papersize":"a4","type":"window"}},{"id":174,"myproperty":null,"enabled":true,"envelope":{"id":1,"quality":"god","papersize":"a4","type":"window"}},{"id":175,"myproperty":null,"enabled":true,"envelope":{"id":1,"quality":"god","papersize":"a4","type":"window"}}]
my ajax call:
self.updateofficeproducts = function() { $.ajax({ url: '/singleletter/getofficeproducts', type: 'post', data: { 'country': self.countryid }, datatype: 'json', success: function (data) { console.log(data.officeproducts); $(data.officeproducts).each(function (index, ele) { alert(ele.id); }); } }); };
so expect iterate 4 different objects, can things ele.id
or ele.enabled
. instead syntax error.
what doing wrong? :-) syntax thing.
use json.parse-
success: function (data) { data = json.parse(data); $(data.officeproducts).each(function (index, ele) { alert(ele.id); }); ...
edit
another thing — i'm not sure of .net, response you're getting not of form expected ajax. string not json-encoded.
you have remove "
before , after [
, ]
, string be-
var = '{"officeproducts": [{\"id\":96,\"myproperty\":null,\"enabled\":true,\"envelope\":{\"id\":1,\"quality\":\"god\",\"papersize\":\"a4\",\"type\":\"window\"}}] }'; // ^here , here^
hope helps.
Comments
Post a Comment