I'm trying to send a broadcast message using a std. html/javascript to Android devices. I have found a script here on stackoverflow but i can't get it to work. it returns no errors. Then a send a message from urban airship admin i works fine. Is i missing something here??
<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>
If the remote server doesn't add an
Access-Control-Allow-Origin
header the AJAX call will then fail the "same origin policy", and the query will be denied.The normal way around this is to modify your URL so jQuery will attempt to use JSONP instead of plain JSON.
You can do this in two ways:
or by adding:
In addition to using
You will also want to use the statusCode parameter. Since you will not be getting valid JSON back, you need to instead check the status code that is passed back to determine if you call was a success: