【问题标题】:urban airship - sending Broadcast message using Json城市飞艇 - 使用 Json 发送广播消息
【发布时间】:2011-11-01 14:09:09
【问题描述】:

我正在尝试使用标准发送广播消息。 html/javascript 到 Android 设备。我在 stackoverflow 上找到了一个脚本,但我无法让它工作。它不返回任何错误。然后从城市飞艇管理员发送一条消息,我工作正常。我在这里遗漏了什么吗??

<script language="JavaScript1.2" type="text/javascript">

var ruleObj = {
    "android": {"alert": "test"}
}
;

var objStr = JSON.stringify(ruleObj);
// username : Application Key;
// password : Application Master Secret;

    jQuery(document).ready(function(jQuery){ 

        jQuery.ajax({
        type: "POST",
        contentType:"application/json",
        username: "qE.........",
        password: "zp........",
        url:"https://go.urbanairship.com/api/push/broadcast/",
        data: objStr,
        success: function(data){
                            alert(data);
            }
        });

    });

</script>

【问题讨论】:

  • "它没有返回错误。"您实际上缺少在 ajax 请求失败时调用的 error(jqXHR, textStatus, errorThrown) 函数。在该函数中,您可以获取特定错误。 jQuery.ajax() 参考文档:api.jquery.com/jQuery.ajax

标签: javascript json urbanairship.com


【解决方案1】:

如果远程服务器未添加Access-Control-Allow-Origin 标头,则 AJAX 调用将使“同源策略”失败,并且查询将被拒绝。

解决这个问题的正常方法是修改你的 URL,这样 jQuery 将尝试使用 JSONP 而不是纯 JSON。

您可以通过两种方式做到这一点:

url: "https://go.urbanairship.com/api/push/broadcast/?callback=?"

或添加:

dataType: 'jsonp'

【讨论】:

  • 谢谢,我已经尝试添加?现在我收到一个 HTTP 错误 405.0 - Method Not Allowed 错误。在UA。故障排除指南 (support.urbanairship.com/customer/portal/articles/…) 它应该与 /.但我用这个网址应该没问题:url:"?go.urbanairship.com/api/push/broadcast"
  • 只需将dataType: 'jsonp' 添加到您的参数中,它应该然后为您附加正确的参数:api.jquery.com/jQuery.ajax
  • 你好。已添加数据类型:'json'。我的萤火虫返回: Object { readyState=4, status=200, statusText="parsererror"} parsererror jQuery161013864902217729813_1320224386941 未被调用。我有点绝望。有没有人有一个可行的例子。
【解决方案2】:

除了使用

dataType: "jsonp",

您还需要使用 statusCode 参数。由于您不会返回有效的 JSON,因此您需要检查传回的状态代码以确定您的调用是否成功:

statusCode: {
    200: function() {
        alert('message sent!');
    },
    500: function(  ) {
        alert('500 error');
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多