有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javascript为什么带有特殊字符的字符串在GET和POST方法中的行为不同?

通过使用jQuery,我试图在后端保存我的字符串数组,下面是我的方法

一,。使用jqueryajax发送数组

var tagTexts = $(ul li.select2-selection__choice")
                   .toArray()
                   .map(function(i){
                       return i.innerText;
                   });
tagTexts = JSON.stringify(tagTexts);
$.ajax({
    type: 'POST' ,
    url: '/tag/tagList',
    dataType: 'json',
    data: {
        tagTexts: tagTexts
    },
    error: function(err) {
       console.log(err);
    },
    success: function(response) {
        //Process Response
    }

});

二,。在后端,它的检索方式如下:

@ResponseBody
@RequestMapping(value = "/tag/tagList", method = RequestMethod.POST)
public String saveTagList(HttpServletRequest request,
                                   @RequestParam(value = "searchTagTexts", required = false)String tagListString) {
   try 
   {
        List<String> tagTexts = (List<String>)mapper.readValue(tagListString, new TypeReference<List<String>>() {});
        System.out.println(tagTexts);
        String response = tagService.saveTags(tagTexts);
        return response;
   } catch (JsonGenerationException e) {
        e.printStackTrace();
   } catch (JsonMappingException e) {
        e.printStackTrace();
   } catch (IOException e) {
        e.printStackTrace();
   }
   return null;
}

在这种情况下,一切正常

使用GET选项尝试相同的场景:

我已经使用GET选项尝试了上述场景(比如在ajax调用中添加类型:“GET”,并在@RequestMapping注释中设置method=RequestMethod.GET)

下面是进行ajax调用时标记文本的值:

"["Drone", "Richard Feynman, PHD", "Yatch","Tik—Tok"]"

在java执行(后端)中,这将显示如下:

enter image description here

我在后端得到了意想不到的字符串。这种行为的原因是什么


共 (0) 个答案