ApacheがインストールされたUbuntuサーバーボックスがあります。私は、オンラインの友達を追跡し、ユーザーがそれらとやり取りできるようにする単純な Web アプリを作成しています。アカウント管理全体のサーバー プラットフォームとして、Commmunigate Pro を使用しています。ximss プロトコルを使用して Communigate に xml リクエストを送信しようとすると、Chrome コンソールに「リクエスト ヘッダー フィールドの Content-Type は Access-Control-Allow-Headers によって許可されていません」というメッセージが表示されます。リクエストにはjavascriptを使用しています。ximss 用の JavaScript クラスがあり、他のサーバーでも正しく動作することがわかっています。関連するコードは次のとおりです:
//index.php
var ximssSession = {
serverName : "192.168.1.254:8100",
theSession : null,
doLogin : function (userName, passWord) {
console.log("doLogin: "+userName + " " + passWord);
var params = {
userName : userName,
password : passWord,
serverName : this.serverName,
loginMethod : "plain",
asyncMode : "syncPOST",
binding : "HTTP",
pollPeriod : 15
};
this.theSession = new XIMSSSession(params, this, this.ximssLoginCompleted);
},
ximssLoginCompleted : function (session, errorCode) {
console.log("ximssLoginCompleted: "+errorCode);
theSession = session;
theSession.setAsyncProcessor(this, this.ximssAsyncAll, null, null, null);
theSession.setAsyncProcessor(this, this.ximssAsyncSession, "session", null, null);
//theSession.setAsyncProcessor(this, this.ximssAsyncPresence, "presence", null, null);
theSession.setNetworkErrorProcessor(this, this.ximssNetworkErrorProcessor, 10);
theSession.start();
var presenceSetRequest = theSession.createXMLNode("presenceSet");
presenceSetRequest.setAttribute("presence", "online");
theSession.sendRequest(presenceSetRequest, this, null, null, false);
var signalBindRequest = theSession.createXMLNode("signalBind");
signalBindRequest.setAttribute("mode", "kill");
theSession.sendRequest(signalBindRequest, this, null, this.ximssOpCompleted, true);
},
ximssAsyncAll : function (xmlData) {
console.log("ximssAsyncAll: " + (new XMLSerializer()).serializeToString(xmlData));
},
ximssAsyncSession : function (xmlData) {
console.log("ximssAsyncSession: " + (new XMLSerializer()).serializeToString(xmlData));
},
ximssAsyncPresence : function (xmlData) {
console.log("ximssAsyncPresence: " + (new XMLSerializer()).serializeToString(xmlData));
},
ximssNetworkErrorProcessor : function (isFatal, timeElapsed) {
console.log("network error");
},
ximssOpCompleted : function (errorCode, xmlData){
console.log("ximssOpCompleted: "+errorCode);
console.log("ximssOpCompleted: " + (new XMLSerializer()).serializeToString(xmlData));
}
};
$(document).ready(function() {
ximssSession.doLogin(
<?php echo "\"" . $_SESSION["userName"] . "\"" ?>,
<?php echo "\"" . $_SESSION["passWord"] . "\"" ?>
);
});
//ximssclient.js
theSessionRef.startHTTPRequest = function(callback,method,url,body,userName,userPassword) {
var httpRequest = new XMLHttpRequest();
if(httpRequest == null) {callback(null,"XMLHttpRequest failed");}
httpRequest.onreadystatechange = function() {
if(httpRequest.readyState != 4) return;
if(httpRequest.aborted) return;
var errorCode = null;
var ximssData = null;
if(httpRequest.status == 0) {
errorCode = "server communication error";
} else if(httpRequest.status == 550) {
errorCode = sessionLost;
} else if(httpRequest.status < 200 || httpRequest.status >= 300) {
if((errorCode = httpRequest.statusText) == null) errorCode = "server protocol error";
} else if(httpRequest.responseXML != null) {
ximssData = httpRequest.responseXML.childNodes[0];
if(ximssData.tagName != "XIMSS") {ximssData = null; errorCode = "server XML response is not XIMSS";}
else if(POSTXMLDocument == null) POSTXMLDocument = httpRequest.responseXML;
} else {
if(httpRequest.responseText != null && httpRequest.responseText != "") errorCode = "server response is not XML";
}
callback(ximssData,errorCode);
}
httpRequest.open(method,url,true,userName,userPassword);
if(typeof(body) == "string") httpRequest.setRequestHeader("Content-Type","text/xml; charset=utf-8");
httpRequest.send(body);
return(httpRequest);
}
これは私のコンソールの始まりです:
doLogin: *** 1234 index.php:19
ximssLoginCompleted: null index.php:34
ximssAsyncSession: <session urlID="94-yzTp9mcoFItifqGR0jBk" userName="***@server.com" random="15C6716184E425E7"/> index.php:59
ximssAsyncAll: <session urlID="94-yzTp9mcoFItifqGR0jBk" userName="***@server.com" random="15C6716184E425E7"/> index.php:55
OPTIONS http://192.168.1.254:8100/Session/94-yzTp9mcoFItifqGR0jBk/sync?reqSeq=1&random=1 Request header field Content-Type is not allowed by Access-Control-Allow-Headers. ximssclient.js:426
XMLHttpRequest cannot load http://192.168.1.254:8100/Session/94-yzTp9mcoFItifqGR0jBk/sync?reqSeq=1&random=1. Request header field Content-Type is not allowed by Access-Control-Allow-Headers. [VM] index.php (223):1
OPTIONS http://192.168.1.254:8100/Session/94-yzTp9mcoFItifqGR0jBk/sync?reqSeq=1&random=2 Request header field Content-Type is not allowed by Access-Control-Allow-Headers. ximssclient.js:426
XMLHttpRequest cannot load http://192.168.1.254:8100/Session/94-yzTp9mcoFItifqGR0jBk/sync?reqSeq=1&random=2. Request header field Content-Type is not allowed by Access-Control-Allow-Headers. [VM] index.php (223):1
OPTIONS http://192.168.1.254:8100/Session/94-yzTp9mcoFItifqGR0jBk/sync?reqSeq=1&random=3 Request header field Content-Type is not allowed by Access-Control-Allow-Headers. ximssclient.js:426
XMLHttpRequest cannot load http://192.168.1.254:8100/Session/94-yzTp9mcoFItifqGR0jBk/sync?reqSeq=1&random=3. Request header field Content-Type is not allowed by Access-Control-Allow-Headers. [VM] index.php (223):1
OPTIONS http://192.168.1.254:8100/Session/94-yzTp9mcoFItifqGR0jBk/sync?reqSeq=1&random=4 Request header field Content-Type is not allowed by Access-Control-Allow-Headers. ximssclient.js:426
XMLHttpRequest cannot load http://192.168.1.254:8100/Session/94-yzTp9mcoFItifqGR0jBk/sync?reqSeq=1&random=4. Request header field Content-Type is not allowed by Access-Control-Allow-Headers. [VM] index.php (223):1
"Header set Acces-Control_Allow_headers" という行を virtualhost ファイルに追加してみましたが、次のようになりました。
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "Content-Type"
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
その行を追加しても違いはありませんでした(もちろんApacheを再起動しました)。ここで何が欠けていますか?
これは私の要求です:
Request URL:http://192.168.1.254:8100/Session/10-G1cSZuOE75Xo0MNCNcbx/sync?reqSeq=1&random=1
Request Method:OPTIONS
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,nl;q=0.2,da;q=0.2
Access-Control-Request-Headers:origin, content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:192.168.1.254:8100
Origin:http://192.168.1.254
Referer:http://192.168.1.254/index.php
User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36
Query String Parametersview sourceview URL encoded
reqSeq:1
random:1
Response Headersview source
Access-Control-Allow-Methods:OPTIONS, GET, HEAD, POST, PUT
Access-Control-Allow-Origin:*
Access-Control-Max-Age:86400
Access-Control-Request-Headers:Origin, Content-Type
Allow:OPTIONS, GET, HEAD, POST, PUT
Connection:keep-alive
Content-Length:0
Content-Type:application/octet-stream
Date:Sun, 14 Jul 2013 21:41:08 GMT
Public:OPTIONS, GET, HEAD, POST, PUT
Server:CommuniGatePro/6.0.5
リクエストにリンクを url としてブラウザに入力するだけで成功します。これらの設定は GET とは異なりますか? また、IE でページを開くと、コンソールにエラーはなく、ximssOpCompleted メソッドの内容に到達します。