带有 jquery 的仪表板跨域 AJAX

2022-01-18 00:00:00 widget dashboard jquery javascript ajax

大家好,我正在为 Apple 的 Dashboard 开发一个小部件,但在尝试使用 jquery 的 ajax 函数从我的服务器获取数据时遇到了问题.这是我的 JavaScript 代码:

Hey everyone, I'm working on a widget for Apple's Dashboard and I've run into a problem while trying to get data from my server using jquery's ajax function. Here's my javascript code:

$.getJSON("http://example.com/getData.php?act=data",function(json) { 
    $("#devMessage").html(json.message)
    if(json.version != version) {
        $("#latestVersion").css("color","red")
    }
    $("#latestVersion").html(json.version)
})

服务器用这个 json 响应:

And the server responds with this json:

{"message":"Hello World","version":"1.0"}

但由于某种原因,当我运行这个时,小部件上的字段不会改变.通过调试,我了解到该小部件甚至不会向服务器发出请求,因此我认为 Apple 设置了某种外部 URL 块.不过我知道这不是真的,因为许多小部件都会打电话回家检查更新.

For some reason though, when I run this the fields on the widget don't change. From debugging, I've learned that the widget doesn't even make the request to the server, so it makes me think that Apple has some kind of external URL block in place. I know this can't be true though, because many widgets phone home to check for updates.

有人对可能出现的问题有任何想法吗?

Does anyone have any ideas as to what could be wrong?

此外,此代码在 Safari 中运行良好.

Also, this code works perfectly fine in Safari.


根据 Luca 的要求,下面是正在运行的 PHP 和 Javascript 代码:


As requested by Luca, here's the PHP and Javascript code that's running right now:

PHP:

echo $_GET["callback"].'({"message":"Hello World","version":"1.0"});';

Javascript:

Javascript:

function showBack(event)
{
var front = document.getElementById("front");
var back = document.getElementById("back");

if (window.widget) {
    widget.prepareForTransition("ToBack");
}

front.style.display = "none";
back.style.display = "block";
stopTime();
if (window.widget) {
    setTimeout('widget.performTransition();', 0);
}
$.getJSON('http://nakedsteve.com/data/the-button.php?callback=?',function(json) { 
    $("#devMessage").html(json.message)
    if(json.version != version) {
        $("#latestVersion").css("color","red")
    }
    $("#latestVersion").html(json.version)
})
}

推荐答案

在 Dashcode 中单击 Widget Attributes 然后 Allow Network Access 确保选中该选项.我已经构建了一些拒绝工作的东西,这就是解决方案.

In Dashcode click Widget Attributes then Allow Network Access make sure that option is checked. I've built something that simply refused to work, and this was the solution.

相关文章