jsonp获取服务器数据的方式

来源:互联网 发布:网络销售股票违法吗 编辑:程序博客网 时间:2024/05/16 06:11
  1. jsonp获取服务器的数据,有两种  
  2. 一,跨域  
  3. 二,不跨域  
  4. 如果跨域  
  5. js的写法有两种  
  6. 1,  
  7. <script type="text/javascript">  
  8.   
  9.   $(function() {  
  10.   $.getJSON('http://localhost:8090/search?jsoncallback=?' ,  
  11.               function(json) {  
  12.                     alert(json);  
  13.                     var html = "";  
  14.                     for (var key in json.data) {  
  15.                         html = html + "<img src='http://localhost:8090/img/99999/o/" + json.data[key].filename + "'><br>";  
  16.                         html = html + "名称:" + json.data[key].filename + "<br>";  
  17.                         html = html + "category:" + json.data[key].category + "<br>";  
  18.                         html = html + "height:" + json.data[key].height + "<br>";  
  19.                         html = html + "width:" + json.data[key].width + "<br>";  
  20.                         html = html + "length:" + json.data[key].length + "<br>";  
  21.                         html = html + "<hr>";  
  22.                     }  
  23.   
  24.                     $('#imageList').html(html);  
  25.                 }  
  26.                 );  
  27.     });  
  28.   
  29. </script>  
  30. get的ur后面有jsoncallback=?  
  31. 这时候,要在服务器端增加如下代码  
  32. hr.getParameter("jsoncallback") +"({jsonp数据的格式})"  
  33. 例子  
  34. jQuery1510062266528242707175_1324369820794({"data":[{"category" : "all","height" : "194","_id" : "4ebce7b5523e7e91029f910a","keyword" : "","width" : "259","chunkSize" : "262144","length" : "9082","md5" : "534a94756fc98a6db0483ee702297a82","filename" : "img001_images_029.jpeg","contentType" : "null","uploadDate" : "Fri Nov 11 18:15:33 JST 2011","aliases" : "null"},{"category" : "all","height" : "194","_id" : "4ebce7b5523e7e91029f9108","keyword" : "","width" : "259","chunkSize" : "262144","length" : "10987","md5" : "7b85b894132f78b2b90cd3fef27317a6","filename" : "img001_images_028.jpeg","contentType" : "null","uploadDate" : "Fri Nov 11 18:15:33 JST 2011","aliases" : "null"}]});  
  35. 注意,是如下格式jQuery1510062266528242707175_1324369820794({xxxx}),要加个()的哦。  
  36.   
  37. 2,get的ur后面有jsoncallback=?的写法可以改为get的ur后面有jsoncallback=getdata  
  38. 同时   function(json) 前要加上getdata=  
  39. 具体如下  
  40. <script type="text/javascript">  
  41.   
  42.   $(function() {  
  43.   $.getJSON('http://localhost:8090/search?jsoncallback=getdata' ,  
  44.               getdata=function(json) {  
  45.                     alert(json);  
  46.                     var html = "";  
  47.                     for (var key in json.data) {  
  48.                         html = html + "<img src='http://localhost:8090/img/99999/o/" + json.data[key].filename + "'><br>";  
  49.                         html = html + "名称:" + json.data[key].filename + "<br>";  
  50.                         html = html + "category:" + json.data[key].category + "<br>";  
  51.                         html = html + "height:" + json.data[key].height + "<br>";  
  52.                         html = html + "width:" + json.data[key].width + "<br>";  
  53.                         html = html + "length:" + json.data[key].length + "<br>";  
  54.                         html = html + "<hr>";  
  55.                     }  
  56.   
  57.                     $('#imageList').html(html);  
  58.                 }  
  59.                 );  
  60.     });  
  61.   
  62. </script>  
  63. 这个时候,服务器端就不需要加上hr.getParameter("jsoncallback")了,直接返回json格式就行了。  
  64.   
  65.   
  66. 具体参见  
  67. http://www.cnblogs.com/5201314/archive/2009/06/23/1509552.html  
  68. http://51mst.iteye.com/blog/1170798  
  69.   
  70. 二,如果是不跨域的,  
  71. 不需要加上jsoncallback,直接如下就行了,同时服务器返回的格式,就是纯jsonp的格式。  
  72. <script type="text/javascript">  
  73.   $(function() {  
  74.   $.getJSON('http://localhost:8090/search' ,  
  75.               function(json) {  
  76.                     alert(1);  
  77.                     var html = "";  
  78.                     for (var key in json.data) {  
  79.                         html = html + "<img src='http://localhost:8090/img/99999/o/" + json.data[key].filename + "'><br>";  
  80.                         html = html + "名称:" + json.data[key].filename + "<br>";  
  81.                         html = html + "category:" + json.data[key].category + "<br>";  
  82.                         html = html + "height:" + json.data[key].height + "<br>";  
  83.                         html = html + "width:" + json.data[key].width + "<br>";  
  84.                         html = html + "length:" + json.data[key].length + "<br>";  
  85.                         html = html + "<hr>";  
  86.                     }  
  87.   
  88.                     $('#imageList').html(html);  
  89.                 }  
  90.                 );  
  91.     });  
  92.   
  93. </script>  
  94. 服务器返回代码  
  95. {"data":[{"category" : "all","height" : "194","_id" : "4ebce7b5523e7e91029f910a","keyword" : "","width" : "259","chunkSize" : "262144","length" : "9082","md5" : "534a94756fc98a6db0483ee702297a82","filename" : "img001_images_029.jpeg","contentType" : "null","uploadDate" : "Fri Nov 11 18:15:33 JST 2011","aliases" : "null"}]}  

原创粉丝点击