Solution to the problem that js front end transmission json background reception ''is converted to quot

  • 2021-09-16 06:17:24
  • OfStack

1. Causes

The front end transmits json format data, but the background receives it, but finds that there is a pile & quot; However, if the background receiving parameters are annotated with @ RequestBody, this problem will not occur. The reason for this problem is that the background does not receive parameters according to json format. The premise of receiving parameters according to json is the request header parameters Content-Type: application/json. In this way, the background framework knows how to process parameters, but sometimes the requirements encountered cannot be written like this, such as sending download requests:
The most common format parameter of json is to send ajax request, but ajax cannot trigger browser download mechanism, so it does not support download;

Send download request with a tag, which carries limited parameters and is suitable for single download request

Use form form splicing parameter, submit form sending request, but can not send request with json parameter, so only json format string splicing to input box, background with String receive, this will appear to translate quotation marks into & quot; This 1 question.

2. Solutions

1. Option 1

Use the method unescapeHtml () under the class org. apache. commons. lang. StringEscapeUtils


@RequestMapping("/downloads")
public ResultVO downloads(String models) {
 String jsonModels = StringEscapeUtils.unescapeHtml(models);
 //  And then turn it into what you want Object
 
 return ResultVO.success();
}

pom Dependence


<dependency>
 <groupId>commons-lang</groupId>
 <artifactId>commons-lang</artifactId>
 <version>2.6</version>
</dependency>

2. Option 2

Will all the & quot Replace Back Quotes


String jsonModels = models.replaceAll("&quot;", "\"");

Step 3 End

Batch downloading has been done, which is quite rewarding. Therefore, if you encounter problems, you still need to patiently analyze and carefully record them. You should not only solve the problems, but also know the root and root.

I would also like to thank you for this article https://blog.csdn.net/charset_ok/article/details/80239882


Related articles: