クライアント: Chrome ブラウザーの JavaScript サーバー: Google App Enging、Java サーブレット サーバー側からトークンを取得しようとしています。これが私の js コードです:
var httpRequest;
if (window.XMLHttpRequest) {
// Mozilla, Safari, Chrome,...
httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) {
// ...
}
if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
httpRequest.open('POST', 'http://myapp.appspot.com/gettoken?userid=ethan', true);
そして私のサーブレットサーバーコード:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userId = request.getParameter("userid");
if (userId != null && !"".equals(userId)) {
String token = createChannel(userId);
writeIntoChannel(response,token);
}
}
基本的に GAE の CodeLabEx4 と同じですが、js コードをクライアント側に移動しているだけです。その元のコードは
httpRequest.open('POST', '/gettoken?userid=ethan', true);
それは機能しますが、完全な URL を追加しても機能しないのはなぜですか? ステータス = 200 ではなく 0
httpRequest.open('POST', 'http://myapp.appspot.com/gettoken?userid=ethan', true);
/////////////////////////////////////////////// //////////////////
いくつかの調査の後、クエリを実行するために ajax() を使用するように更新しました。
$.ajax({
url : 'http://myapp.appspot.com/gettoken?userid=xxx',
type : "POST",
data:null,
success : function(data) {
alert('ok!');
},
error: function(data) {
alert(data.statusText);
},
complete : function() {
//alert("always");
},
});
しかし、返された data.statusText は「エラー」です。