Javascript 如何在ubuntu apache上允许内容类型标头

Javascript 如何在ubuntu apache上允许内容类型标头,javascript,apache,http,google-chrome,ubuntu,Javascript,Apache,Http,Google Chrome,Ubuntu,我有一个安装了apache的Ubuntu服务器盒。我正在制作一个简单的web应用程序,它可以跟踪在线朋友,让用户与他们互动。我使用Commmunigate Pro作为整个帐户管理的服务器平台。当我尝试使用ximss协议向Communigate发送xml请求时,chrome控制台中的Access Control Allow header不允许获取请求头字段内容类型。我正在使用javascript进行请求。ximss有一个javascript类,我知道它可以在其他服务器上正常工作。以下是相关代码:

我有一个安装了apache的Ubuntu服务器盒。我正在制作一个简单的web应用程序,它可以跟踪在线朋友,让用户与他们互动。我使用Commmunigate Pro作为整个帐户管理的服务器平台。当我尝试使用ximss协议向Communigate发送xml请求时,chrome控制台中的Access Control Allow header不允许获取请求头字段内容类型。我正在使用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"] . "\"" ?>
    );
});

如果我只是在浏览器中以url的形式在请求中输入链接,则会成功。GET的这些设置是否不同?另外,当我在IE中打开页面时,控制台中没有错误,它到达了ximssOpCompleted方法的内容。

您启用CORS或使用JSONP,因为问题实际上不是标题,而是同源策略。@adeneo我也在我的virtualhosts文件中添加了行“header set Access Control Allow origin*”,这不要紧吗?我如何启用CORS?@adeneo我考虑了你的建议,但它确实不正确,因为我没有跨域请求任何内容。所有内容都在同一台服务器apache和Communigate上。
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
<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>
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