jquery getJSON 跨域问题

2024-06-28

使用 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 . ');';
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

jquery getJSON 跨域问题 的相关文章

  • EXT JS中有全局变量吗

    在 java 和 C 中 我们可以全局存储变量并从项目中的任何位置访问它的值 比如说 我在一个名为Residence我正在保存residenceNumber这是一个INT到一个名为的全局变量houseNumberGlobalVariable
  • 在 NodeJs 中使用 Jest 模拟 Http Post 请求

    我有一个使用本机 https request 使用 Azure Function 内的 Node Js 构建的外部 POST API 一切正常 现在我正在尝试构建单元测试用例 并对模拟 Request 方法感到震惊 回调响应具有 on 功能
  • Karma Webpack - 错误:找不到模块“./test/utilities.js”

    我正在使用 Karma Webpack 进行项目的单元测试 当我跑步时karma start 我有这个错误 Error Cannot find module test utilities js at myproject test campa
  • 将 Babel 与单个输出文件和 ES6 模块一起使用

    这是我的 gulp 任务 将 ES6 代码编译成单个 ES5 文件 我使用类和模块 import export 在 ES6 中 gulp src paths scripts pipe sourcemaps init pipe babel p
  • Ajax 请求返回 HTTP 错误 500,使用 MVC 和 $.ajax 调用 POST?

    我已经看到了几个关于此的主题 并且我已经尝试了所有答案 ASP NET MVC JsonResult 返回 500 https stackoverflow com questions 3113952 asp net mvc jsonresu
  • GSON:如何从 Json 中获取不区分大小写的元素?

    下面显示的代码在以下情况下效果很好JSON对象包含jsonKey因为它被传递给该方法 我想知道 是否有一种方法可以获取分配给不区分大小写的键表示的值 Example public String getOutputEventDescripti
  • 在没有 Flash 的情况下用 HTML / js 录制音频?

    从麦克风录制音频并将 ajax 发回服务器 真的不知道如何在 Flash 中做到这一点 并希望它保持简单 目前还没有浏览器实现WHATWG 设备 API http www whatwg org specs web apps current
  • 使用 javascript 和 jQuery UI datepicker 获取工作日数

    我有两个日期选择器 我可以从中计算天数 而无需计算星期六和星期日 但我想在周五和周六这样做 我尝试过一些不同的方法但失败了 对我来说 处理不包括周六和周日的天数很容易 但处理周五和周六的天数则不然 以下是我的 javascript 代码 f
  • Google 闭包编译器使用 WebStorm

    我喜欢用谷歌闭包编译器 https developers google com closure compiler in WebStorm https www jetbrains com webstorm 我已经通过 npm 下载了它 npm
  • 在浏览器开发工具中看不到SignalR流量

    我构建了使用 SignalR 的服务器和客户端代码 该网站运行完美 但我无法在任何浏览器 chrome IE Firefox 中看到网络流量 我知道网络流量在那里 因为该网站正在运行 有没有办法在浏览器中查看 SignalR 流量 如果不是
  • 如何在 Electron 中使窗口大小响应。 (打开应用程序时)

    我最近开始在 Electron 上制作一个应用程序 我想让窗口具有响应能力 例如 如果我在不同的屏幕上打开应用程序 它应该根据屏幕尺寸以全尺寸打开 我的代码 app on ready gt const htmlPath path join
  • Django 管理中的自定义依赖下拉菜单

    我有一个按阶段模型的项目外键 我很难在 Django 管理页面中创建依赖的下拉列表 我想当用户从该项目的 项目下拉 阶段选择一个项目时 在第二个下拉菜单中显示 实现这一目标的最佳方法是什么 如果下拉列表根据其父级的值来过滤项目 那就太好了
  • 链接index.html client.js 和 server.js

    我从 Node js 开始 我的第一个程序已经遇到了问题 下面是我正在使用的代码 索引 html
  • Chrome 扩展程序导致 DOM 闪烁,该扩展程序会在页面加载之前删除 DOM 元素

    我正在编写一个 chrome 扩展 我想在页面加载之前删除内容 例如this http tech blog tomchambers me 2016 01 13 How to write a simple page rewriting Chr
  • 我以为我了解范围

    有人可以告诉我为什么 x 的最后一次记录等于0而不是1 我想因为它是在函数之外声明的 所以它具有全局范围 然后在函数中它的值设置为1并且该值将保持原样一个全球性的 我知道函数内的第一个 x 值是全局的 因为没有 var 关键字声明的任何变量
  • Backbone.View:delegateEvents 未将事件重新绑定到子视图

    我已将这个问题分解为尽可能小的示例 即 它只是为了演示问题 不一定代表现实世界的场景 假设我有一个父视图 此处为 MainView 其中包含一个子视图 此处为 SubView 如果在任何时候我需要重新渲染父视图 从而重新渲染子视图 我就会丢
  • td 内的文本可以随 td 一起调整大小吗? (是其高度的百分比?)

    我有一个 100 占屏幕的表格 里面有 3x3 9 个单元格 每个单元格占 33 到目前为止 一切都很好 这是一个干净 可扩展的井字棋游戏 问题在于 TD 内的文本不会随比例缩放 有没有办法说 我希望文本大小为 TD 高度的 30 或者其他
  • Firebase 停止监听 onAuthStateChanged

    从版本 3 0 0 开始 我很难删除身份验证状态更改侦听器 要根据文档启动侦听器 firebase auth onAuthStateChanged function user handle it 但是 我在文档中找不到任何涉及删除身份验证状
  • 什么是标志变量?

    最近我遇到了标志变量 但我不知道它们的作用 我不太确定何时使用标志变量以及如何使用它 我用 Google 搜索了它 但没有任何与我的上下文 JavaScript 相关的具体示例 标记变量的定义和使用 http www javascriptk
  • 如何防止外部 CSS 添加和覆盖 ReactJS 组件样式

    我有一个自定义的 ReactJS 组件 我想以某种方式设置样式 并将其作为插件提供给许多不同的网站 但是 当网站使用全局样式 Twitter bootstrap 或其他 css 框架 时 它会添加并覆盖我的组件的样式 例如 全局 css l

随机推荐

  • Django 如何查看生成的 SQL 查询?

    我有一个需要数据并应该将其插入数据库的表单 当我处理该表单时 它给了我一个值错误 但是当我进入数据库并尝试手动插入它时 它工作正常 为了调试这种情况 我想看看 Django 生成的查询失败了 在调试网页上我没有看到类似 SQL 查询的内容
  • Flutter 中桌面的条件包导入

    我知道的可用性dart library html可以作为网页的条件 但是桌面呢 尽管我认为如果您创建并使用一个新包 其中为不同平台指定了不同的包 您可以为桌面导入特定的包 而不是为移动设备导入特定的包 pubspec yaml如中所解释的该
  • Haskell 中的模式匹配正则表达式模式

    在 Scala 中 我有一个正则表达式模式匹配 如下所示 val Regex d 4 d 2 d 2 r val Regex year month day 2013 01 06 结果是 year String 2013 month Stri
  • 使用 Laravel Eloquent 关系构建三元关系

    拥有三个实体 Project Employee 就业 问题描述 员工可以从事多个项目 每个项目都有一份工作 我想要访问所有项目以及您推荐的某个员工的就业情况 我不确定 但这种关系一定看起来像ternary 物理表尚未定义 因此 可以自由地设
  • 如何在 Windows 上设置 Node.js 的工作目录?

    我刚刚安装了 Windows 版的 Node js 运行它真的是轻而易举 我想将它用作构建过程的一部分 将多个文件组合在一起 如下所示 settings var FILE ENCODING utf 8 EOL n DIST FILE PAT
  • 删除高图表上的导出和打印按钮插件

    我正在使用 MVC 目前正在使用 highchart 我正在使用 Exporting js 以便用户可以打印或导出 highchart 图表 我的视图中有两个图表 我想禁用其中一个图表的打印和导出 我怎样才能做到这一点 Exporting
  • 生成的 Protobuf 代码导致应用程序崩溃

    我正在尝试使用 Google 的 Protocol Buffers 来实现应用程序的保存文件 准备 已创建一个简单的测试 proto 文件来测试功能 message LessonFile optional string creator 1
  • 如何在 Firefox 中动态单击 href 链接?预期方法仅适用于 IE

    http jsfiddle net HVGre 1 http jsfiddle net HVGre 1 测试链接 我的页面上有一个 html 链接 我需要能够动态单击该链接 这在 IE 中与 click 函数配合良好 但在 Firefox
  • 在 Python 中使用 LaTeX 表示法格式化数字

    在Python中使用格式字符串我可以轻松地以 科学记数法 打印数字 例如 gt gt print g 1e9 1e 09 将数字格式化为 LaTeX 格式 即 1 times10 09 的最简单方法是什么 The siunitx http
  • 按受欢迎程度列出 PyPI 包 [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 是否可以获得按受欢迎程度 总下载数 排序的 PyPI 软件包列表 我无法找到在 PyPI 上执行此操作
  • UWP:找不到组件(HRESULT 异常:0x88982F50)

    当我尝试在 uwp 应用程序中拍照时 有时会出现错误 此错误很难重现 并且是几分钟前在桌面平台上发生的 我使用悬停摄像机来检索图像缓冲区 public async Task
  • 没有调试适配器,无法发送“变量 VSCODE”

    在调试控制台中写道 调试器侦听 ws 127 0 0 1 55624 c4f74411 66ec 44b2 9cf6 15efb60f8611 如需帮助 请参阅 https nodejs org en docs inspector http
  • 运行 EventSources 对服务器的影响

    我目前正在构建一个聊天只是为了好玩 我以前从未这样做过 我一般都是为了尝试一下EventSource API Server Sent Events http www html5rocks com en tutorials eventsour
  • puma 初始化程序不适用于 Rails 4.2

    我之前安装过很多次puma 从来没有出现过这个问题 我逐字遵循heroku 的指示 我创建了一个Procfile里面有这个 web bundle exec puma C config puma rb 这是配置文件 puma rb worke
  • IIS 中的 URL 重写得到 404

  • 如何计算CPU核心频率

    我正在尝试使用 RDTSC 但似乎我的方法获取核心速度可能是错误的 include stdafx h include
  • NestedScrollView 的 smoothScrollTo() 行为很奇怪

    我想要实现的是滚动到scroll position 1 when tab1 等等 被点击像this https material io design components tabs html image https 3A 2F 2Fstor
  • Paypal 沙盒“待定”多币种

    我目前使用 PayPal Rest api 执行付款时 它返回为 待处理 其说明的原因是 多货币 这背后的原因是因为默认的 协助者 帐户设置为美国 我需要它全部位于英国 问题是我可以登录 电子邮件受保护 cdn cgi l email pr
  • 是什么阻止用户应用程序“劫持”到内核模式?

    据我了解 内核模式是一种硬件功能 前任 它可以通过寄存器设置 value1 gt 内核模式 value2 gt 用户模式 当内核加载并运行用户应用程序时 用户应用程序应通过系统调用与内核通信以执行特权操作 在此期间将发生中断 执行将切换到内
  • jquery getJSON 跨域问题

    使用 JQuerys getJSON 从另一个域拉入 JSON 文件时 我似乎无法使该文件正常工作 我已经将回调部分放在了 url 的末尾 但仍然没有任何乐趣 Firebug 告诉我这是一个跨域问题 这似乎是有道理的 就好像我将 json