也许我错过了一些真正基本的东西..但我无法弄明白。
我有server.php作为我的GCM通知服务器。当我在终端(MAC)中将此server.php称为
时$ php server.php
,它的作品完美。
这是我的 server.php
<?php
$data = array( 'message' => 'Hello!' );
$ids = array('APA91...'); //my actual registered ID from android device would go here
sendGoogleCloudMessage( $data, $ids );
function sendGoogleCloudMessage( $data, $ids )
{
// key from Google APIs Console
$apiKey = 'AIzaS...'; //my actual API key would go here
$url = 'https://android.googleapis.com/gcm/send';
$post = array(
'registration_ids' => $ids,
'data' => $data,
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );
// Actually send the push!
$result = curl_exec( $ch );
// Error? Display it!
if ( curl_errno( $ch ) )
{
echo 'GCM error: ' . curl_error( $ch );
}
curl_close( $ch );
echo $result;
}
?>
现在,我在服务器计算机中安装了这个server.php代码,我试图通过jquery mobile中的ajax调用此函数,如下所示:
$.ajax({
async: false,
type: "POST",
url: "http://myServer.com/server.php",
data: {"data":sentData}, //i have var sentData setup in actual code
cache: false,
success: function(data){
alert(data);
}
});
但是当我尝试使用$ .ajax触发它时,相同的工作代码不起作用...... 我在这做错了什么?