jQuery解析/显示来自php的json数据json_encode [英] jQuery parse/display json data from php json_encode

查看:214
本文介绍了jQuery解析/显示来自php的json数据json_encode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jquery中的初始.ajax调用:

Initial .ajax call in jquery:

$.ajax({                                      
 type: 'post',
 url: 'items_data.php',                  
 data: "id="+id,                                       
 dataType: 'json',                     
 success: function(data){     
  if(data){
   make_item_rows(data);
  }else{
   alert("oops nothing happened :(");
  }        
 } 
});     

将简单的字符串发送到php文件,如下所示:

Sends a simple string to a php file which looks like:

header('Content-type: application/json');
require_once('db.php');

if( isset($_POST['id'])){
 $id = $_POST['id'];
}else{
 echo "Danger Will Robinson Danger!";
}

$items_data = $pdo_db->query ("SELECT blah blah blah with $id...");
$result_array = $items_data->fetchAll();
echo json_encode($result_array);

我很好地捕获了$ result_array并将其传递给另一个函数.我再次检查是否确实返回了正确的值,因为我可以将结果回显到页面上,并且显示类似以下内容:

I am catching the $result_array just fine and passing it on to another function. I double checked that there is indeed proper values being returned as I can just echo result to my page and it displays something like the following:

[{"item_id":"230","0":"230","other_id":"700"},{"item_id":"231","0":"231","other_id":"701"},{"item_id":"232","0":"232","other_id":"702"}]

我在弄清楚如何遍历结果时遇到麻烦,因此无法将值注入到HTML中的表中.这是我的功能:

I am having trouble figuring out how to iterate through the results so I can inject values into a table I have in my HTML. Here is what I have for my function:

function make_item_rows(result_array){  
 var string_buffer = "";
 $.each(jQuery.parseJSON(result_array), function(index, value){
  string_buffer += value.item_id; //adding many more eventually
  $(string_buffer).appendTo('#items_container');
  string_buffer = ""; //reset buffer after writing          
 });                    
}

我还尝试在$ .each函数中放置一个警报,以确保它触发了3次,实际上是.但是,没有数据来自我的代码.也尝试了其他一些方法,但是没有运气.

I also tried putting an alert in the $.each function to make sure it was firing 3 times, which it was. However no data comes out of my code. Have tried some other methods as well with no luck.

更新:我将代码更改为包括parseJSON,没有骰子.我的jquery文件中出现意外的令牌错误(尝试使用本机json解析器时正确).试图添加json标头无济于事,同样的错误. jQuery版本1.9.1.现在的代码应该在上面反映出来.

UPDATE: I changed my code to include the parseJSON, no dice. Got an unexpected token error in my jquery file (right when it attempts to use native json parser). Tried adding the json header to no avail, same error. jquery version 1.9.1. Code as it is now should be reflected above.

推荐答案

在ajax调用中设置dataType:"JSON"和回调.

Set the dataType:"JSON" and callback in your ajax call.

例如:

$.ajax({
    url: "yourphp.php?id="+yourid,
    dataType: "JSON",
    success: function(json){
        //here inside json variable you've the json returned by your PHP
        for(var i=0;i<json.length;i++){
            $('#items_container').append(json[i].item_id)
        }
    }
})

还请考虑在您的PHP中设置JSON内容类型. header('Content-type: application/json');

Please also consider in your PHP set the JSON content-type. header('Content-type: application/json');

这篇关于jQuery解析/显示来自php的json数据json_encode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆