1

I have the following code that retrieves Json data:

   <script type="text/javascript">
   $(document).ready(
   function(){
   $.getJSON(
     './json.txt',
     function(data){

        for(i=0; i<data.length; i++){
           var  content  = '<li>';
               content +=  data[i].fname + ' ' + data[i].lname;
               content += '</li>';

           $('ul.rubrica').append(content);
        }

     }
  );
}
);
</script>
<ul class="rubrica">
</ul>

And the json data:

[
   {
      "fname"     : "<a href='http://www.riccardo.it'>Piottino</a>",
      "lname"     : "Mr Potato"
   }

]

Now I have the json in another server: http://www.site.com/json.txt How can I use jsonP to get the content like I did before? Tnx in advance

--Edit: Since I see I have to use a server side language, how can I do it with asp.net?

Attila
  • 702
  • 1
  • 12
  • 34
  • possible duplicate of [JavaScript: How do I create JSONP?](http://stackoverflow.com/questions/1678214/javascript-how-do-i-create-jsonp) – Felix Kling Oct 20 '11 at 08:19

2 Answers2

1

Details @ http://api.jquery.com/jQuery.getJSON/

Example -

$.getJSON("http://www.site.com/json.txt?jsoncallback=?",
  function(data) {
        for(i=0; i<data.length; i++){
           var  content  = '<li>';
               content +=  data[i].fname + ' ' + data[i].lname;
               content += '</li>';

           $('ul.rubrica').append(content);
});
Jayendra
  • 52,349
  • 4
  • 80
  • 90
0

You cannot do it with a stand alone JSON file.

When the JQUERY makes a cross domain call for JSON using jsonp, it sends a parameter in the form of a number in the REQUEST parameters array. The JSON that is returned should then be the value of this parameter.

SO you need a php or asp file that reads the REQUEST parameter and gets the value of the parameter (name of parameter i've forgotten- do a print of REQUEST parameter and find it).

e.g. if the parameter value you got in the php REQUEST is 1245563

then your JSON output should be

EDIT:

1245563 = "[
              {
              "fname"     : "<a href='http://www.riccardo.it'>Piottino</a>",
              "lname"     : "Mr Potato"
              }
              ]";
Anand
  • 4,182
  • 6
  • 42
  • 54
  • What you are talking about is not JSONP: https://secure.wikimedia.org/wikipedia/en/wiki/JSONP – Felix Kling Oct 20 '11 at 08:28
  • Tnx, how can I do it with asp.net ? – Attila Oct 20 '11 at 08:35
  • It might work in the sense that it does not throw a syntax error, but it is still not JSONP. It won't work, e.g. together with jQuery. The point of standards or protocols is to follow them. – Felix Kling Oct 20 '11 at 08:36
  • @Attila: Follow the link to the question I choose as duplicate. It gives you an example how to do this in a server side language (PHP instead of asp.net, but I hope you get the idea). – Felix Kling Oct 20 '11 at 08:43