Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I'm facing an issue all of a sudden recently with the below AJAX snippet.
JavaScript
var obj = $.get(serviceUrl + "?q=" + keyword, function (data, status) {
  if (status == 'success') {
    $.each(data, function (index, item) {
      returnString = item.keymatchterm;
      returnString = encodeURIComponent(returnString);
    });
  }
}, "json");


Sample Response Output: data = "[{"keymatchterm":"Axis"}]", status = "success"

When using the $.each, the data is getting iterated as

index = 0, item = "[" rather than {"keymatchterm":"Axis"}

I'm not able to find out what could be the reason for this issue.
Any suggestions on this would greatly help.

Thanks in advance

What I have tried:

Initially the data type was not included as a part of $.get method. I later added it just to make sure the expected data type is JSON. I'm not sure whether should we explicitly use JSON Parse() method to parse the response. The response is already in JSON.
Posted
Updated 9-Mar-16 2:45am

1 solution

your object data appears to be a string.

try :
$.each(eval(data), function (index, item) {
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900