Java 无法在“XMLHttpRequest”上执行“setRequestHeader”[...] 不是有效的 HTTP 标头字段值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24326885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Failed to execute 'setRequestHeader' on 'XMLHttpRequest' [...] is not a valid HTTP header field value
提问by Marius Manastireanu
I am developing an application with AngularJS (client side) and Java (server side) using RESTful services (with Jersey).
我正在使用 RESTful 服务(使用 Jersey)开发带有 AngularJS(客户端)和 Java(服务器端)的应用程序。
In the client I have something like this
在客户端我有这样的事情
$http({
url : "/Something/rest/...",
method : "POST",
headers : {
...
'text' : text
}
});
on the server side my Java method header looks like this
在服务器端,我的 Java 方法标头如下所示
public JSONObject myMethod(@HeaderParam("text") String text [...]) throws JSONException
And I'm getting this error when trying to send a request
我在尝试发送请求时收到此错误
Error: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'Some text here formatted with \n and \t' is not a valid HTTP header field value.
Is this because of the formatting? Is it because the text is quite long?
这是因为格式吗?是不是因为文字很长?
What should I change? Please guide me
我应该改变什么?请指导我
采纳答案by Liberat0r
Are you sending your 'text' in headers on purpose? This is in general not a place for doing this. For sending content through POST(or GET) you should use 'data' field. So:
您是故意在标题中发送“文本”吗?这通常不是执行此操作的地方。要通过 POST(或 GET)发送内容,您应该使用“数据”字段。所以:
$http({
url : "/Something/rest/...",
method : "POST",
data: {
...
'text' : text
}
});
,and on the server side similar change for getting your data from POST. You don't need and shouldn't mess with headers unless you know what you are doing.
,并在服务器端从 POST 获取数据的类似更改。除非您知道自己在做什么,否则您不需要也不应该弄乱标题。
回答by Santosh Kadam
I had faced this problem. Please make sure that in your code header name doesn't contain space. i.e. 'designId ' is invalid, as it contain space at end
我曾经遇到过这个问题。请确保您的代码标头名称中不包含空格。即“designId”无效,因为它在末尾包含空格