0

I send js variable to Spring controller by clicking button. That's what I have in js:

function submitArticle()
{
    var pdata= $('textarea').froalaEditor('html.get');
    $.post("/submitProject", pdata).done(function(response) {
        console.log("Response: " + pdata);
    });
}

So it works well, console.log displays next : <h1>New Article</h1><p>Some text</p>

but, that's what I get in Spring controller:

%3Ch1%3ENew+Article%3C%2Fh1%3E%3Cp%3ESome+text%3C%2Fp%3E=

It just replaces <, >, and / to some codes. How to replace them to normal tags. Because I want to store this html code in java String.

My Spring Controller:

@PostMapping("/submitProject")
public ModelAndView submitProject(@RequestBody String html, @ModelAttribute(value = "LoggedUser") User user)
{
    System.out.println(html);
    return new ModelAndView("redirect:/");
}
Clijsters
  • 4,031
  • 1
  • 27
  • 37
Dan Durnev
  • 89
  • 3
  • 12
  • 1
    It's encoded HTML. You have to decode it on the server side. I would have guessed Spring would take care of it; you're doing something else wrong. You should not want to store markup. It's the data that matters. – duffymo Feb 05 '18 at 12:34
  • @duffmo I offer a user an froala editor to create article with headers, paragraphs, images etc. It translates to html and store as a string in mongodb, then it just pastes to blank html page. That's how creating articles should work for me. That's why I need to store html as a string. – Dan Durnev Feb 05 '18 at 12:40
  • Then decode it as HTML and store it: https://stackoverflow.com/questions/994331/java-how-to-unescape-html-character-entities-in-java – duffymo Feb 05 '18 at 12:40

2 Answers2

1

Check out the java.net.URLDecoder#decode method. When I ran the code you posted through it I was able to retrieve the original text

Jewels
  • 966
  • 11
  • 28
0

Send as an object and receive a Map

$.post("/submitProject", {pdata})...

and

public ModelAndView submitProject(@RequestBody Map<String, String> data ...
// -> String html = data.get('pdata');
baao
  • 71,625
  • 17
  • 143
  • 203