gpt4 book ai didi

javascript - jquery getJSON 跨域问题

转载 作者:行者123 更新时间:2023-12-03 22:20:08 26 4
gpt4 key购买 nike

使用 JQuerys getJSON 从另一个域拉入 JSON 文件时,我似乎无法使该文件正常工作。我已经将回调部分放在了 url 的末尾,但仍然没有任何乐趣。 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) {
$('<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");
} else {
$('<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");
}
});
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"                }                           ]           ]}

最佳答案

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 . ');';

关于javascript - jquery getJSON 跨域问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/760993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com