简体   繁体   English

在浏览器中解析HTTP请求标头以及400错误请求时出错

[英]Error parsing HTTP request header along with 400 bad request in browser

My scenario is, 我的情况是

I have a text field with a button named as "Search", when I entered a value in the text field and click on search button, the value should write/written in existing JSON file. 我有一个带有名为“搜索”的按钮的文本字段,当我在文本字段中输入值并单击搜索按钮时,该值应在现有JSON文件中写入/写入。 Please find the below code. 请找到下面的代码。

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 code 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();
}

I got the below error by using this code. 通过使用此代码,出现以下错误。

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)

Also I got 404 Bad Request in Browser console. 另外,我在浏览器控制台中收到404错误请求。 Kindly help me to fix this issue. 请帮助我解决此问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM