は私です:AJAXリクエスト - 私はここに、私のローカルサーバーにAJAX要求を送信するPOSTリクエスト
function getNewsFromDB() {
var httpUrl = "http://10.0.0.2:8080/Rosh/" + 'Get_News';
$.ajax({
type: 'POST',
url: httpUrl,
data: '{"MESSAGE_TYPE":"GET_NEWS","VERSION": "1","CITY":"ROSH_HAAIN"}',
dataType: 'json',
crossDomain: true,
success: function(data) {
window.alert("success");
},
error: function(xhr,textStatus,err) {
window.alert(JSON.stringify(xhr) + " ... " + textStatus + " ... " + err);
}
});
}
また、私は、要求を処理するために、このサーバー側のコードを持っている:
res.setContentType("application/json");
try
{
res.addHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "*");
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With,Host,User-Agent,Accept,Accept-Language,Accept-Encoding,Accept-Charset,Keep-Alive,Connection,Referer,Origin");
res.setHeader("Access-Control-Max-Age", "8080");
res.setCharacterEncoding("utf-8");
res.getWriter().write(response);
}
catch (IOException e)
{
// TODO Auto-generated catch block
}
コードを実行すると、次のようなブラウザのダイアログが表示されます。 「準備完了状態:0、応答テキスト:」、ステータス:0、ステータステキスト:「エラー」
このダイアログは、リクエストは私のサーバーに届いていません。
また、私はこのエラーを見た:
XMLHttpRequest cannot load http://10.0.0.2:8080/Rosh/Get_News . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:8000 ' is therefore not allowed access. The response had HTTP status code 404.
誰かが私のコードで何が間違っている任意のアイデアを持っていますか?
応答に 'Access-Control-Allow-Origin'ヘッダーが設定されていません。あなたのコードにはそのコードが含まれているようです。 –
サーバーはどのように実装されていますか? –
あなたはそれを答えで追加できますか?私は今どこにどのように追加するのか... – foo