【问题标题】:Error parsing HTTP request header along with 400 bad request in browser在浏览器中解析 HTTP 请求标头以及 400 错误请求时出错
【发布时间】:2018-01-09 04:47:55
【问题描述】:

我的情况是,

我有一个带有名为“搜索”的按钮的文本字段,当我在文本字段中输入一个值并单击搜索按钮时,该值应该写入/写入现有的 JSON 文件。请找到以下代码。

function search_criteria()
{
var inputData = document.getElementById("sel").value;
var fileDetails = 
{
    'fileName'  : 'sample.json',
    'fieldName' : 'field1',
    'inputValue' : inputData
}
$
.ajax({
    type : "GET",
    url : "/abc/rest/input_value/search_action",
    data : JSON.stringify(fileDetails),
    contentType : 'application/json',
    success : function(msgr) {
    },
    error : function(data) {
        alert("Inside error while adding input in search field"
                + data);
    }
});

}

Java 代码

@GET
@Path("/search_action")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response search_action(@Context HttpServletRequest request, 
JSONObject inputJsonObj)
        throws JSONException, FileNotFoundException, IOException {

    String relativeWebPath = "/TEMP/scenario_content/" + 
inputJsonObj.get("fileName").toString();
    String absoluteDiskPath = 
request.getServletContext().getRealPath(relativeWebPath);

    File file = new File(absoluteDiskPath);
    if(!file.exists())
    {
        file.createNewFile();
    }
    JSONObject json = new JSONObject();
    json.put(inputJsonObj.getString("fieldName"), 
inputJsonObj.getString("inputValue"));

    FileWriter fw = new FileWriter(absoluteDiskPath);
    fw.write(json.toString());

    return Response.status(Status.OK).entity("success").type(MediaType.APPLICATION_JSON)
            .header("X-Content-Type-Options", "nosniff").build();
}

我使用此代码得到以下错误。

org.apache.coyote.http11.AbstractHttp11Processor 
process
INFO: Error parsing HTTP request header
Note: further occurrences of HTTP header parsing errors will be logged at 
DEBUG level.
java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
at org.apache.coyote.http11.AbstractNioInputBuffer.parseRequestLine(AbstractNioInputBuffer.java:283)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1045)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1533)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1489)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

我在浏览器控制台中也收到了 404 Bad Request。请帮我解决这个问题。

【问题讨论】:

  • 404 是“未找到”,400 是“错误请求”。那么是 404 错误,还是错误的请求错误?
  • 你给代码@Path("/search_criteria") 但是ajax调用url:"/abc/rest/input_value/searchAction"。你可以为java日志设置DEBUG级别吗?写的是错误记录在DEBUG中。此外,请查看浏览器网络调试选项卡以检查您的请求。
  • 错字.. 它的 400 Bad Request 不是 404..

标签: javascript java jquery ajax rest


【解决方案1】:

在您显示的代码中,Controller 的路径是 @Path("/search_criteria"),但在您的 ajax 调用中,您的 url 是 url : "/abc/rest/input_value/searchAction",,因此它们不会计算,您会得到 404。

【讨论】:

  • 更改为 /abc/rest/input_value/search_criteria 时出现同样的错误
  • 你试过在浏览器中写url吗?也许不是 ajax 问题。
  • 我已通过更改 java 代码中的 POST 方法来纠正错误。以前我使用过 GET 方法。
  • 但该值未附加到现有的 json 文件中,即使我在浏览器控制台中收到 200 OK 消息。
猜你喜欢
  • 2017-01-16
  • 2016-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多