javascript – Jquery getJSON跨域问题

前端之家收集整理的这篇文章主要介绍了javascript – Jquery getJSON跨域问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当使用 JQuerys getJSON从另一个域中拉出时,我似乎无法让我的JSON文件工作.我把回调部分放在了URL的末端,但仍然没有喜悦. Firebug告诉我一个跨域问题,这似乎是有道理的,好像我将本地的json文件放在下面的代码(不包括?jsoncallback =?工作正常)

属于Jquery部分

  1. $.getJSON("http://anotherdomain/js/morearticles.js?jsoncallback=?",function(json){
  2. if (show5More.nextSetCount < json.items.length) { // Check not on last group of data
  3. $('#lineupswitch li').hide(); // Hide the existing items
  4. $.each(json.items,function(key,value){ // Loop over the returned data from the json file
  5. if (key === show5More.nextSetCount) { // If the itteration is equal to the datablock continure
  6. show5More.nextSetCount = show5More.nextSetCount + 1; //
  7. $(value).each( function(index) {
  8. if( (index % 2) == 0) {
  9. $('<li class="even ' + this.cname +'"><a href="' + this.href + '" class="lineup-thumb"><img src="' + this.thumbimg + '" /></a><h3><a href="' + this.href + '">' + this.titletext + '</a></h3><p>' + this.paratext + '</p></li>').appendTo("#lineupswitch");
  10. } else {
  11. $('<li class="odd ' + this.cname +'"><a href="' + this.href + '" class="lineup-thumb"><img src="' + this.thumbimg + '" /></a><h3><a href="' + this.href + '">' + this.titletext + '</a></h3><p>' + this.paratext + '</p></li>').appendTo("#lineupswitch");
  12. }
  13. });
  14. return false;
  15. }
  16. });
  17. }
  18. });
  19. }

和我已经验证的JSON.

  1. {
  2. "items": [
  3. [
  4. {
  5. "href": "/edinburgh/video/news-090415-s2-squalor-edinburgh/","thumbimg": "http://brightcove.vo.llnwd.net/d7/unsecured/media/1486976045/1486976045_19721015001_asset-1239819553334.jpg?pubId=1486976045","titletext": "Cannabis plants found in house with neglected children","paratext": "A court has heard four young children lived in","cname": ""
  6. },{
  7. "href": "/edinburgh/video/news-090414-s2-waverley-station-edinburgh/","thumbimg": "http://brightcove.vo.llnwd.net/d7/unsecured/media/1486976045/1486976045_19537855001_asset-1239732920496.jpg?pubId=1486976045","titletext": "Multi-million pound revamp for Waverley Station","paratext": "Edinburgh's Waverley Station is set for a",{
  8. "href": "/edinburgh/video/news-s2-natal-20090408/","thumbimg":"http://brightcove.vo.llnwd.net/d7/unsecured/media/1486976045/1486976045_18948154001_asset-1239206353135.jpg?pubId=1486976045","titletext": "Stillbirth charity on the road to raise awareness","paratext": "SANDS Lothian are hoping to highlight their",{
  9. "href": "/edinburgh/video/news-090407-l2-rbs/","thumbimg":"http://brightcove.vo.llnwd.net/d7/unsecured/media/1486976045/1486976045_18827378001_asset-1239110600777.jpg?pubId=1486976045","titletext": "Thousands of jobs to go at Royal Bank of Scotland","paratext": "Edinburgh-based bank to cut 4,500 positions in the",{
  10. "href": "/edinburgh/video/news-090415-s2-squalor-edinburgh/","titletext": "1","cname": "lastlineup"
  11. }
  12. ],[
  13. {
  14. "href": "/edinburgh/video/news-090415-s2-squalor-edinburgh/","titletext": "2","paratext": "3","paratext": "4","cname": "lastlineup"
  15. }
  16.  
  17. ]
  18.  
  19. ]
  20. }
  21.  
  22. {
  23. "items": [
  24. [
  25. {
  26. "href": "/edinburgh/video/news-090415-s2-squalor-edinburgh/","cname": "lastlineup"
  27. }
  28.  
  29. ]
  30.  
  31. ]
  32. }

解决方法

altCognito是正确的.这是一个工作示例:

把它放在你打电话的页面的顶部

  1. $.getJSON("http://www.yourotherdomain.com/testcross.PHP?jsoncallback=?",function(data){
  2. $('body').html(data.name).css("color","green");
  3. });

PHP将返回的东西:

  1. $data = '{"name" : "hello world"}';
  2. echo $_GET['jsoncallback'] . '(' . $data . ');';

猜你在找的jQuery相关文章