tags:

views:

1681

answers:

5

I want a variable of the json file to be displayed (Date) but it does not seen to work. What am I doing wrong?

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

         });        
    });

</script>
A: 

You should have a parser on the client that understands date format the date isn’t parsed and .getJSON() won’t parse it for you.

Sorantis
A: 

$.getJSON takes 3 arguments in the order url, data, callback. So in this case, you would do:

$(document).ready(function(){
 $.getJSON("http://json-head.appspot.com/?url=http://www.trinum.com/ibox/chatel/images/photofull.jpg&amp;callback=?", null, function(data){
  $.each(data.headers, function(i,item){
   if(i < 2){
    $("body").append("+item.Date+");
   }
  });
});
});
defrex
it should work with two either
Artem Barger
A: 

Just google for javascript+dump and you will find some equivalents of php's print_r for javaScript.

This is one example (from http://www.openjs.com/scripts/others

/dump_function_php_print_r.php )
/**
 * Function : dump()
 * Arguments: The data - array,hash(associative array),object
 *    The level - OPTIONAL
 * Returns  : The textual representation of the array.
 * This function was inspired by the print_r function of PHP.
 * This will accept some data as the argument and return a
 * text that will be a more readable version of the
 * array/hash/object that is given.
 * Docs: http://www.openjs.com/scripts/others/dump_function_php_print_r.php
 */
function dump(arr,level) {
    var dumped_text = "";
    if(!level) level = 0;

    //The padding given at the beginning of the line.
    var level_padding = "";
    for(var j=0;j<level+1;j++) level_padding += "    ";

    if(typeof(arr) == 'object') { //Array/Hashes/Objects 
     for(var item in arr) {
      var value = arr[item];

      if(typeof(value) == 'object') { //If it is an array,
       dumped_text += level_padding + "'" + item + "' ...\n";
       dumped_text += dump(value,level+1);
      } else {
       dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
      }
     }
    } else { //Stings/Chars/Numbers etc.
     dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
    }
    return dumped_text;
}
elcuco
A: 

is your client-side sent as Date ? because sometimes i found that instead of sending as String, it render as javascript Date (it is timestamp)

so you need to convert like this:

var yourDate = new Date(item.Date);

the complete code:

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

         });        
    });

</script>
nightingale2k1
+3  A: 
CMS