简体   繁体   English

仪表板跨域AJAX与jquery

[英]Dashboard Cross-domain AJAX with jquery

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. 嘿大家,我正在为Apple的Dashboard创建一个小部件,我在尝试使用jquery的ajax函数从我的服务器获取数据时遇到了问题。 Here's my javascript code: 这是我的javascript代码:

$.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)
})

And the server responds with this json: 服务器用这个json响应:

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

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. 从调试开始,我了解到小部件甚至没有向服务器发出请求,因此我认为Apple有一些外部URL阻塞。 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? 有没有人对什么可能是错的有任何想法?

EDIT: Also, this code works perfectly fine in Safari. 编辑:此外,此代码在Safari中完美运行。


As requested by Luca, here's the PHP and Javascript code that's running right now: 根据Luca的要求,这里是正在运行的PHP和Javascript代码:

PHP: PHP:

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)
})
}

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) }) } 

In Dashcode click Widget Attributes then Allow Network Access make sure that option is checked. 在Dashcode中单击“ 小组件属性”,然后单击“ 允许网络访问”,确保选中该选项。 I've built something that simply refused to work, and this was the solution. 我已经建立了一些拒绝工作的东西,这就是解决方案。

Cross-domain Ajax requests ( Using the XMLHttpRequest / ActiveX object ) are not allowed in the current standard, as per the W3C spec : 根据W3C规范 ,当前标准中不允许跨域Ajax请求(使用XMLHttpRequest / ActiveX对象):

This specification does not include the following features which are being considered for a future version of this specification: 此规范不包括以下针对此规范的未来版本考虑的功能:

  • Cross-site XMLHttpRequest; 跨站点XMLHttpRequest;

However there's 1 technique of doing ajax requests cross-domain, JSONP , by including a script tag on the page, and with a little server configuration. 然而,通过在页面上包含脚本标记以及一些小服务器配置,有一种通过跨域JSON执行ajax请求的技术。

jQuery supports this , but instead of responding on your server with this jQuery支持这个 ,但不是用你的服务器响应

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

you'll want to respond with this: 你会想回应这个:

myCallback({"message":"Hello World","version":"1.0"});

myCallback must be the value in the "callback" parameter you passed in the $.getJSON() function. myCallback必须是您在$ .getJSON()函数中传递的“callback”参数中的值。 So if I was using PHP, this would work: 所以,如果我使用PHP,这将工作:

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

Apple has some kind of external URL block in place. Apple有一些外部URL阻止。

In your Info.plist you need to have the key AllowNetworkAccess set to true. 在Info.plist中,您需要将密钥AllowNetworkAccess设置为true。

<key>allowNetworkAccess</key>
<true/>

Your code works in Safari because it is not constrained in the dashboard sever and it is not standards complient in that it DOES allow cross site AJAX. 您的代码在Safari中工作,因为它不受仪表板服务器的约束,并且它不符合标准,因为它允许跨站点AJAX。 FF IS standards complient in that it DOES NOT allow cross site ajax. FF IS标准的重要性在于它不允许跨站点ajax。

If you are creating a dashboard widget, why don't you use the XMLHttpRequest Setup function in the code library of DashCode. 如果要创建仪表板窗口小部件,为什么不在DashCode的代码库中使用XMLHttpRequest安装程序功能。 Apple built these in so you don't need to install 3rd party JS libraries. Apple构建了这些,因此您无需安装第三方JS库。 I'm not sure about JSON support but perhaps starting here will lead you in a better direction. 我不确定JSON的支持,但也许从这里开始会带领你走向更好的方向。

Interesting that it works in Safari. 有趣的是它在Safari中有效。 As far as I know to do x-domain ajax requests you need to use the jsonp dataType. 据我所知做x-domain ajax请求你需要使用jsonp dataType。

http://docs.jquery.com/Ajax/jQuery.getJSON http://docs.jquery.com/Ajax/jQuery.getJSON

http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/ http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/

Basically you need to add callback=? 基本上你需要添加callback=? to your query string and jquery will automatically replace it with the correct method eg: 查询字符串和jquery会自动用正确的方法替换它,例如:

$.getJSON("http://example.com/getData.php?act=data&callback=?",function(){ ... });

EDIT: put the callback=? 编辑:把callback=? bit at the end of the query string just to be safe. 位于查询字符串末尾只是为了安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM