当前位置:  开发笔记 > 编程语言 > 正文

Jquery getJSON跨域问题

如何解决《JquerygetJSON跨域问题》经验,为你挑选了2个好方法。

我似乎无法使用JQuerys getJSON从其他域中提取我的JSON文件.我已将回调部分放在网址的末尾,但仍然没有快乐.Firebug告诉我它是一个跨域问题,这似乎有意义,好像我将json文件本地放在下面的代码中(不包括?jsoncallback =?工作正常)

继承人Jquery部分

$.getJSON("http://anotherdomain/js/morearticles.js?jsoncallback=?",
    function(json){
        if (show5More.nextSetCount < json.items.length) { // Check not on last group of data
            $('#lineupswitch li').hide();  // Hide the existing items    
            $.each(json.items, function(key,value){ // Loop over the returned data from the json file
                if (key === show5More.nextSetCount) {  // If the itteration is equal to the datablock continure
                     show5More.nextSetCount = show5More.nextSetCount + 1; // 
                     $(value).each( function(index) {
                         if( (index % 2) == 0) {
                             $('
  • ' + this.titletext + '

    ' + this.paratext + '

  • ').appendTo("#lineupswitch"); } else { $('
  • ' + this.titletext + '

    ' + this.paratext + '

  • ').appendTo("#lineupswitch"); } }); return false; } }); } }); }

    和JSON,我已经验证过.

    { 
      "items": [
                [
                    {
                        "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": ""
                    },
                    {
                        "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",
                        "cname": ""
                    },
                    {
                      "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",
                      "cname": ""
                    },
                    {
                      "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",
                      "cname": ""
                    },
                    {
                        "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": "1",
                        "paratext": "A court has heard four young children lived in",
                        "cname": "lastlineup"
                    }               
                ],
    
                [
                    {
                        "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": "1",
                        "paratext": "A court has heard four young children lived in",
                        "cname": ""
                    },
                    {
                        "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": "2",
                        "paratext": "Edinburgh's Waverley Station is set for a",
                        "cname": ""
                    },
                    {
                      "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": "3",
                      "cname": ""
                    },
                    {
                      "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": "4",
                      "cname": ""
                    },
                    {
                      "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",
                      "cname": "lastlineup"
                    }               
    
                ]   
    
            ]
    }
    
    { 
      "items": [
                [
                    {
                        "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": ""
                    },
                    {
                        "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",
                        "cname": ""
                    },
                    {
                      "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",
                      "cname": ""
                    },
                    {
                      "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",
                      "cname": ""
                    },
                    {
                        "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": "1",
                        "paratext": "A court has heard four young children lived in",
                        "cname": "lastlineup"
                    }               
                ],
    
                [
                    {
                        "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": "1",
                        "paratext": "A court has heard four young children lived in",
                        "cname": ""
                    },
                    {
                        "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": "2",
                        "paratext": "Edinburgh's Waverley Station is set for a",
                        "cname": ""
                    },
                    {
                      "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": "3",
                      "cname": ""
                    },
                    {
                      "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": "4",
                      "cname": ""
                    },
                    {
                      "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",
                      "cname": "lastlineup"
                    }               
    
                ]   
    
            ]
    }
    

    Thomas.. 12

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

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

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

    和将返回东西的PHP:

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


    cgp.. 11

    它没有返回回调函数,因此无法进行评估.

    你应该在你的PHP代码中有这样的东西:

    echo $_GET['callback'] . '(' . $jsonData . ');';
    

    嗯,对不起,如果你使用的是php,那就是这种情况.

    你想在你的javascript文件中使用它,并且你需要指定一个硬编码回调,因为它是一个静态列表.

    请参阅:http: //www.ibm.com/developerworks/library/wa-aj-jsonp1/



    1> Thomas..:

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

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

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

    和将返回东西的PHP:

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



    2> cgp..:

    它没有返回回调函数,因此无法进行评估.

    你应该在你的PHP代码中有这样的东西:

    echo $_GET['callback'] . '(' . $jsonData . ');';
    

    嗯,对不起,如果你使用的是php,那就是这种情况.

    你想在你的javascript文件中使用它,并且你需要指定一个硬编码回调,因为它是一个静态列表.

    请参阅:http: //www.ibm.com/developerworks/library/wa-aj-jsonp1/

    推荐阅读
    mobiledu2402852413
    这个屌丝很懒,什么也没留下!
    DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
    Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有