douyan1972
2013-11-09 07:16
浏览 62

jquery-ajax中的parseJSON toJSON响应不起作用

I am new to AJAX using jquery. I have a json response as below:

[{"customer_name":"Customer A"},{"customer_name":"Customer B"},{"customer_name":"Customer C"}]

And my ajax file is :

 function(result){
    $('#resdiv').html(result);
    console.log(result);
    var json_obj = $.parseJSON(result);//parse JSON
    alert(json_obj);

    var output="<ul>";
    for (var i in json_obj)
    {
    output+="<li>" + json_obj[i].customer_name + "</li>";
    }
    output+="</ul>";
    $('#resdiv1').html(output);
}

Though I can view the JSON response in div id resdiv, the div id resdiv1 is empty ! Also alert(json_obj); does not alerts anything ! Whats wrong with the file ?

NB: I am learning from Zuch Tutorial

图片转代码服务由CSDN问答提供 功能建议

我是使用jquery的AJAX新手。 我有一个json响应如下:

  [{“customer_name”:“Customer A”},{“customer_name”:“Customer B”},{“customer_name”:  “客户C”}] 
   
 
 

我的ajax文件是:

  function(result){
  $('#resdiv')。html(result); 
 console.log(result); 
 var json_obj = $ .parseJSON(result); //解析JSON 
 alert(json_obj); 
 
 var  output =“&lt; ul&gt;”; 
 for(var i in json_obj)
 {
 output + =“&lt; li&gt;”  + json_obj [i] .customer_name +“&lt; / li&gt;”; 
} 
输出+ =“&lt; / ul&gt;”; 
 $('#resdiv1')。html(输出); 
} \  n   
 
 

虽然我可以在div id resdiv 中查看JSON响应,但div id resdiv1 是空的! 此外 alert(json_obj); 不会发出任何警报! 该文件有什么问题?

注意:我正在学习 Zuch教程

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doudeng5218 2013-11-09 07:24
    已采纳

    You dont need to parse the json again.Simply do a iteration and try like this

    var json = [{"customer_name":"Customer A"},{"customer_name":"Customer B"},{"customer_name":"Customer C"}];
    
    var output="<ul>";
    $.each(json,function(key,val){
      output+="<li>" + val.customer_name + "</li>";
    });
    output+="</ul>";
    
    console.log(output);
    

    See DEMO

    打赏 评论
  • duanmaifu3428 2013-11-09 07:35

    Can you try this

       function(result){ 
          $('#resdiv').html(result); 
          console.log(result); 
          var json_obj = $.parseJSON(result);
          //parse JSON alert(json_obj);
    
          var output="<ul>"; 
           $.each(json_obj,function(key,val){ 
               output+="<li>" + val.customer_name + "</li>"; 
               console.log(key+":::"+val);
          });
            output+="</ul>";
    
         $('#resdiv1').html(output); 
      }
    
    打赏 评论
  • douchuza8908 2013-11-09 07:57

    check this out

    // Need to parse if string.
    var result = '[{"customer_name":"Customer A"},{"customer_name":"Customer B"},{"customer_name":"Customer C"}]';
    var json_obj = $.parseJSON(result);//parse JSON
    
    // No need to parse
    var json_obj = [{"customer_name":"Customer A"},{"customer_name":"Customer B"},{"customer_name":"Customer C"}];
    

    Also check this

    // if undefined, you don't have resdiv1 div or you have call function before your div render.
    alert($('#resdiv1').html());
    
    打赏 评论

相关推荐 更多相似问题