Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过jquery显示json变量_Jquery_Ajax_Json - Fatal编程技术网

通过jquery显示json变量

通过jquery显示json变量,jquery,ajax,json,Jquery,Ajax,Json,我希望显示json文件的一个变量(日期),但它不起作用。我做错了什么 <script type="text/javascript"> $(document).ready(function() { $.getJSON("http://json-head.appspot.com/?url=http://www.trinum.com/ibox/chatel/images/photofull.jpg&callback=?", function(data)

我希望显示json文件的一个变量(日期),但它不起作用。我做错了什么

<script type="text/javascript">
    $(document).ready(function()
   {
        $.getJSON("http://json-head.appspot.com/?url=http://www.trinum.com/ibox/chatel/images/photofull.jpg&callback=?", function(data)
         {
           $.each(data.headers, function(i,item)
           {
              if(i < 2)
              {
                 $("body").append("+item.Date+");
              }
           });

         });        
    });

</script>

$(文档).ready(函数()
{
$.getJSON(“http://json-head.appspot.com/?url=http://www.trinum.com/ibox/chatel/images/photofull.jpg&callback=?,函数(数据)
{
$.each(data.headers,函数(i,项)
{
如果(i<2)
{
$(“正文”)。追加(“+item.Date+”);
}
});
});        
});

客户端上应该有一个解析器,能够理解日期格式。日期未被解析,.getJSON()不会为您解析日期。

$。getJSON按url、数据、回调的顺序接受3个参数。因此,在这种情况下,您将执行以下操作:

$(document).ready(function(){
 $.getJSON("http://json-head.appspot.com/?url=http://www.trinum.com/ibox/chatel/images/photofull.jpg&callback=?", null, function(data){
  $.each(data.headers, function(i,item){
   if(i < 2){
    $("body").append("+item.Date+");
   }
  });
});
});
$(文档).ready(函数(){
$.getJSON(“http://json-head.appspot.com/?url=http://www.trinum.com/ibox/chatel/images/photofull.jpg&callback=?,null,函数(数据){
$.each(data.headers,函数(i,项){
如果(i<2){
$(“正文”)。追加(“+item.Date+”);
}
});
});
});

只需在google上搜索javascript+转储,您就会发现php的print\r for javascript的一些等价物

这是一个例子(来自)

/dump\u function\u php\u print\u r.php)
/**
*函数:dump()
*参数:数据数组、哈希(关联数组)、对象
*级别-可选
*返回:数组的文本表示形式。
*此函数的灵感来自PHP的print_r函数。
*这将接受一些数据作为参数,并返回一个
*文本,该文本将是更具可读性的
*给定的数组/哈希/对象。
*文件:http://www.openjs.com/scripts/others/dump_function_php_print_r.php
*/
功能转储(arr,电平){
var_text=“”;
如果(!level)level=0;
//在行首给出的填充。
var级别_=”;

对于(var j=0;j您的客户端是否作为日期发送? 因为有时我发现它不是作为字符串发送,而是作为javascript日期(它是时间戳)呈现

因此,您需要这样转换:

var yourDate = new Date(item.Date);
完整代码:

<script type="text/javascript">
    $(document).ready(function()
   {
        $.getJSON("http://json-head.appspot.com/?url=http://www.trinum.com/ibox/chatel/images/photofull.jpg&callback=?", function(data)
         {
           $.each(data.headers, function(i,item)
           {
              if(i < 2)
              {
                 $("body").append(new Date(item.Date));
              }
           });

         });        
    });

</script>

$(文档).ready(函数()
{
$.getJSON(“http://json-head.appspot.com/?url=http://www.trinum.com/ibox/chatel/images/photofull.jpg&callback=?,函数(数据)
{
$.each(data.headers,函数(i,项)
{
如果(i<2)
{
$(“正文”)。追加(新日期(项目日期));
}
});
});        
});

实际的问题是您的JSON响应结构,
标题
只是一个对象,使用该函数,您正在迭代此对象成员(内容长度、Via等)。如果
标题
要存储多个对象,则应在其上使用数组符号“
[]
”:

通过这样做,将遍历
headers
数组中定义的整个对象

数组表示法以
[
(左括号)开始,以
]
(右括号)结束,值之间用
(逗号)分隔:


有关JSON语法和结构检查的更多信息。

在For..in循环中添加hasOwnProperty检查,效果非常好。我可以将它与jquery.terminal一起使用,将JSON对象输出到控制台中,以便调试工具。
<script type="text/javascript">
    $(document).ready(function()
   {
        $.getJSON("http://json-head.appspot.com/?url=http://www.trinum.com/ibox/chatel/images/photofull.jpg&callback=?", function(data)
         {
           $.each(data.headers, function(i,item)
           {
              if(i < 2)
              {
                 $("body").append(new Date(item.Date));
              }
           });

         });        
    });

</script>
{
    "status_code": 200, 
    "ok": true, 
    "headers": [{
        "Content-Length": "7068", 
        "Via": "HTTP\/1.1 GWA", 
        "X-Powered-By": "ASP.NET", 
        "Accept-Ranges": "bytes", 
        "X-Google-Cache-Control": "remote-fetch", 
        "Server": "Microsoft-IIS\/6.0", 
        "Last-Modified": "Tue, 06 Feb 2007 07:57:38 GMT", 
        "ETag": "\"8b5f5c78c449c71:2c6a\"", 
        "Cache-Control": "no-cache", 
        "Date": "Sun, 19 Jul 2009 05:51:42 GMT", 
        "Content-Type": "image\/jpeg"
    }]
};