无法使用Closure库将标头Content-Type更改为application / json。此xhr.send()已预检。我已经在服务器端进行了所需的配置。但是从客户端以某种方式,我无法对Content-Type进行更改,它始终保持为空。有什么办法可以强制标题更改而不是此代码?
xhr.headers.set('Content-Type','application / json');
Http-Method: OPTIONS
Content-Type:
Headers: {Accept=[*/*], Accept-Charset=[ISO-8859-1,utf-8;q=0.7,*;q=0.3], accept-encoding=[gzip,deflate,sdch], Accept-Language=[en-US,en;q=0.8], Access-Control-Request-Headers=[origin, content-type], Access-Control-Request-Method=[POST], connection=[keep-alive], Content-Type=[null], Host=[localhost:8181], Origin=[http://localhost], Referer=[http://localhost/?], User-Agent=[Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19 (KHTML, like Gecko) Ubuntu/12.04 Chromium/18.0.1025.168 Chrome/18.0.1025.168 Safari/535.19]}
Response-Code: 200
Content-Type: text/xml
Headers: {Allow=[POST, GET, OPTIONS, HEAD], Date=[Wed, 11 Jul 2012 16:31:04 GMT], Content-Length=[0]}
最佳答案
我认为应该将标头添加到opt_headers映射中的xhr.send()函数中:
goog.net.XhrIo.prototype.send = function(url,opt_method,opt_content,opt_headers)
@param {string | goog.Uri}网址Uri发出请求。
@param {string =} opt_method发送方法,默认:GET。
@param {string | GearsBlob =} opt_content发布数据。如果基础HTTP请求对象是Gears HTTP请求,则这可能是Gears Blob。
@param {Object | goog.structs.Map =} opt_headers要添加到请求中的标头的映射。
所以:
var xhr = new goog.net.XhrIol
var headers = new goog.structs.Map;
headers.set('Content-Type','application/json')
xhr.send(uri, 'OPTIONS', null, headers);
类似的东西对我们来说很好...
问候,
雷内