0

I have a json encoded result as

{  
    "result":[  
        {  
            "CODE":"STC\/R\/935",
            "WAY":"In",
            "DATE":"2016-02-19",
            "TYPE":"Re-Entry",
            "TKTP":"NA",
            "TIME":"2016-02-23 17:52:37"
        },
        {  
            "CODE":"5\/105",
            "WAY":"In",
            "DATE":"2016-01-30",
            "TYPE":"Re-Entry",
            "TKTP":"NA",
            "TIME":"2016-02-23 17:52:37"
        },
        {  
            "CODE":"356",
            "WAY":"In",
            "DATE":"2016-02-06",
            "TYPE":"Re-Entry",
            "TKTP":"NA",
            "TIME":"2016-02-23 17:52:37"
        },
        {  
            "CODE":"FCC\/ETC\/01",
            "WAY":"In",
            "DATE":"2016-02-10",
            "TYPE":"Re-Entry",
            "TKTP":"NA",
            "TIME":"2016-02-23 17:52:37"
        },
        {  
            "CODE":"3\/739",
            "WAY":"In",
            "DATE":"2016-02-03",
            "TYPE":"Re-Entry",
            "TKTP":"NA",
            "TIME":"2016-02-23 17:52:37"
        }
    ]
}

When i tried to iterate through it in javascript it is not excecuted. Hope somebody help. I already spent one full day googling about it.

function bringdata(){
    $.ajax({
        url:'report_entry.php',
        type:"POST",
        data:{nameV:$('#fq').val()},
        async: false,
        success: function(data){                
            $.each(data, function(i,post){
                $("#magix").append("<li>+post.CODE+</li>");
            });
        }
    });
}
2
  • 1
    json is invalid jsonformatter.curiousconcept.com ...also it should be "<li>" + post.CODE + "</li> Commented May 12, 2016 at 18:56
  • @LoganMurphy i did validation with that site. but all elements are got same like.Still could you pls recomend way to get it in standard from jsonencode ? Commented May 12, 2016 at 19:06

3 Answers 3

1

You aren't concatenating your strings correctly.

$("#magix").append("<li>" + post.CODE + "</li>");

If you don't close off the quote marks correctly, you'll just make a string with +s in it.

Also, if your data matches the posted JSON, then you need to access the result property of data to get your array.

$.each(data.result, function(i, post) {
  $("#magix").append("<li>" + post.CODE + "</li>");
});
Sign up to request clarification or add additional context in comments.

Comments

0

Fix these lines

data = JSON.parse(data);
$.each(data.result, function(i,post){
    $("#magix").append("<li>"+post.CODE+"</li>");
});

Or without using jquery $.each,

data = JSON.parse(data);
data.result.forEach(function(post){
    $("#magix").append("<li>"+post.CODE+"</li>");
});

4 Comments

it is not fixed. Im sorry
@Bineesh Did you try ,parsing data first. post = JSON.parse(post)
thanks for that words. it works. googled and thought. but still have some concerns. will see.
great.. happy to hear that
0

With help of @zohaib ijaz, i could correct it like :

function bringdata() {

  $.ajax({
    url: 'report_entry.php',
    type: "POST",
    data: {
      nameV: $('#fq').val()
    },
    async: false,
    success: function(data) {
      var divition = $('#magix');
      var bine = "bineesh";
      var count = 0;
      obj = JSON.parse(data);
      $.each(obj.result, function(i, post) {
        //alert("success");
        divition.append('<li>Name:' + obj.result[count].CODE + '</li>');
        count = count + 1;

      });
    }

  });
}

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.