为什么要转换为小写?在Firefox中它是:"X-Requested-With".在IE中,它表示为:"x-requested-with"
我有一个Play 2.0.1应用程序,我想使用托管在其他域上的Javascript调用.我的Javascript调用失败了:
Origin http://mydomain.com is not allowed by Access-Control-Allow-Origin.
Run Code Online (Sandbox Code Playgroud)
我已经找到了一些如何在Play 1中设置正确的HTTP标头的例子,但是没有为Play 2.0.1找到任何东西.在阅读文档(http://www.playframework.org/documentation/2.0.2/JavaResponse)之后,我尝试了以下方法以使工作正常:
public static Result myJsonWebService() {
...
response().setHeader("Access-Control-Allow-Origin", "*");
return ok(toJson(jsonObject));
}
Run Code Online (Sandbox Code Playgroud)
但我的JS Web服务调用仍然失败.
为了让这个工作,我需要做什么?
我的Servlet不会使用UTF-8进行JSON响应.
MyServlet.java:
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws Exception {
PrintWriter writer = res.getWriter();
res.setCharacterEncoding("UTF-8");
res.setContentType("application/json; charset=UTF-8");
writer.print(getSomeJson());
}
}
Run Code Online (Sandbox Code Playgroud)
但是特殊字符没有显示出来,当我查看我在Firebug中找回的标题时,我明白了Content-Type: application/json;charset=ISO-8859-1.
我grep -ri iso .在我的Servlet目录中做了一个,并且什么都没有,所以我没有明确地将类型设置为ISO-8859-1.
我还应该指定我在Eclipse中的Tomcat 7上以J2EE目标作为开发环境运行它,使用Solaris 10以及他们称之为Web服务器环境的任何人(其他人管理它)作为生产环境,行为是相同.
我还确认提交的请求是UTF-8,只有响应是ISO-8859-1.
我修改了代码以反映我在设置字符编码之前调用PrintWriter.我从原来的例子中省略了这一点,现在我意识到这是我问题的根源.我在这里读到你必须在调用之前设置字符编码HttpServletResponse.getWriter(),否则getWriter会为你设置它为ISO-8859-1.
这是我的问题.所以上面的例子应该调整为
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws Exception {
res.setCharacterEncoding("UTF-8");
res.setContentType("application/json");
PrintWriter writer = res.getWriter();
writer.print(getSomeJson());
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在客户端使用slimide和angularJS实现一个简单的授权.为了测试REST APi,我正在使用一个名为Rested for Mac的程序,它允许发送其余的呼叫.
一旦授权完成,我想在每次休息时调用一个jwt令牌,该令牌可以在slim内用于授权某些路径的请求.
现在我通过Rested传递以下标题和正文:
Accept: */*
Accept-Encoding: gzip, deflate
Content-Type: application/json
Authorization: jwt-test
Accept-Language: de-de
{
"login": "TestLogin",
"password": "TestPassword",
"uuid": "dsfglj45690dfgkl456"
}
Run Code Online (Sandbox Code Playgroud)
而不只是打印出整个标题:
Slim\Http\Headers Object ( [data:protected] => Array ( [Host] => localhost:8888
[Content-Type] => application/json [Content-Length] => 89 [Connection]
=> keep-alive [Accept] => */* [User-Agent] => Rested/2009 CFNetwork/673.4
Darwin/13.4.0 (x86_64) (iMac13%2C2) [Accept-Language] => de-de [Accept-Encoding]
=> gzip, deflate ) )
Run Code Online (Sandbox Code Playgroud)
如您所见,此数组中没有授权.
我也直接用firefox检查了这个,结果相同.O可以使用firebug查看请求标头中的Authorization字符串,但它不在slimframework的dumped数组中.
有没有人提示我的问题所在?
在此先感谢您的亲切问候
solick
我试图Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders在iOS 7中设置我的自定义HTTP标头,但我没有运气.
这在iOS 8中运行良好.
有没有人有什么建议?
在我们的Angular应用程序中,我们需要解析一些$ http的响应头.
例如,我们需要解析一些带X前缀的响应头X-Total-Results: 35.
打开Network浏览器开发工具的选项卡并检查相对于$ http请求的资源,我验证了响应头X-Total-Results: 35是否存在.
在浏览器中,X-Total-Results标头可用,但无法在Angular $ http中解析.
有没有办法在$ http中访问'raw'响应并为头文件编写自定义解析器?
$http.({method: 'GET', url: apiUrl,)
.then( function(response){
console.log('headers: ', response.headers());
console.log('results header: ', response.headers('X-Total-Results'));
// ...
})
Run Code Online (Sandbox Code Playgroud)
控制台输出
headers: Object {cache-control: "no-cache="set-cookie"", content-type: "application/json;charset=utf-8"}
results header: null
Run Code Online (Sandbox Code Playgroud) 在Debian 8.2上使用Apache 2.4,我试图启用所有css和js文件的缓存.缓存图像效果很好; 也就是说,浏览器收到304状态,因此不会再次下载.但我无法缓存其他文件.
我在虚拟主机文件中使用它:
<IfModule mod_expires.c>
<FilesMatch "\.(jpe?g|png|gif|js|css)$">
ExpiresActive On
ExpiresDefault "access plus 1 week"
</FilesMatch>
</IfModule>
Run Code Online (Sandbox Code Playgroud)
该expires模块已启用.我确实重启了apache,清理了浏览器cookie等.没有成功.
来自浏览器开发人员工具的gif图像响应:
Cache-Control:max-age=604800
Connection:Keep-Alive
Date:Wed, 25 Nov 2015 21:37:50 GMT
ETag:"4174a-4e69c97fbf080"
Expires:Wed, 02 Dec 2015 21:37:50 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.4.10 (Debian)
Run Code Online (Sandbox Code Playgroud)
css文件的响应:
Accept-Ranges:bytes
Cache-Control:max-age=604800
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:135
Content-Type:text/css
Date:Wed, 25 Nov 2015 21:37:50 GMT
ETag:"5116-525639d271c78-gzip"
Expires:Wed, 02 Dec 2015 21:37:50 GMT
Keep-Alive:timeout=5, max=99
Last-Modified:Wed, 25 Nov 2015 20:50:52 GMT
Server:Apache/2.4.10 (Debian)
Vary:Accept-Encoding
Run Code Online (Sandbox Code Playgroud)
看起来expires标题设置正确,但浏览器一直在请求文件(200 OK).
我试过Chrome和Firefox.
摘要:
1.)当我按照网站内的链接时,浏览器使用缓存的文件.但是当我按F5时,他们会重新下载css和js文件,但是他们不会重新下载图像.图像给304.那很好.
2.)当我按下Ctrl-F5时,自然就会重新下载所有文件.那也没关系.
3.)所以问题是如何为其他文件启用缓存(就像图像一样).为什么apache会区分图像和其他文件?我没有在配置文件中为图像添加任何特殊内容.
问:如何正确启用css和js文件的缓存? …
我们已经配置了nginx服务器来将缓存控制头添加到公共和到期时间为1个月.但是当我们从浏览器发出请求时,客户端正在添加Cache-control:no-cache标头.因此,每次都是新鲜的内容,而不是提供缓存版本.
我怎样才能缓存静态文件?
如果我从终端触发请求,则服务器添加的缓存控制头在响应头中可见.
我需要向 HTTP 请求添加自定义标头,其值的类型将为布尔值。但是,HTTP 标头值都是字符串。那么在这种情况下表示布尔值的常见做法是什么?0/ 1,false/ true,False/ True,off/ on, ETC?
例如
My-Header: 1
Run Code Online (Sandbox Code Playgroud)
或者一般推荐什么,即最常用的约定?
我遇到了 Chrome 的情况,经过测试87.0.4280.88,在某些情况下 Chrome 会发送sec-ch-ua和sec-ch-ua-mobile请求标头,尽管chrome://flags/#enable-experimental-web-platform-features设置为“禁用”。
例如:
sec-ch-ua: "Google Chrome";v="87", " Not;A Brand";v="99", "Chromium";v="87"
sec-ch-ua-mobile: ?0
Run Code Online (Sandbox Code Playgroud)
发生这种情况时,摆脱这些标头的唯一方法是使用参数启动 Chrome --disable-features=UserAgentClientHint。
有人对此有解释吗?据我了解,这些功能仍处于实验阶段,规范正在进行中(https://wicg.github.io/ua-client-hints/)。
http-headers ×10
javascript ×3
http ×2
ajax ×1
alamofire ×1
angularjs ×1
apache ×1
browser ×1
client-hints ×1
encoding ×1
httpresponse ×1
ios7 ×1
ios8 ×1
java ×1
jquery ×1
json ×1
rest ×1
servlets ×1
slim ×1
swift ×1