1

我有一个安装了 apache 的 Ubuntu 服务器盒。我正在制作一个简单的网络应用程序来跟踪在线朋友并让用户与他们互动。我使用Communigate Pro 作为整个账户管理的服务器平台。当我尝试使用 ximss 协议向 Communigate 发送 xml 请求时,我在 chrome 控制台中收到“Access-Control-Allow-Headers 不允许请求标头字段 Content-Type”。我正在为请求使用 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 *: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 方法的内容。

4

0 回答 0