0
votes
<script type="text/javascript"
        src="https://cdnjs.cloudflare.com/ajax/libs/froala-editor/2.7.6/js/froala_editor.pkgd.min.js"></script>
        <script>
        $(function() {
            $('#edit').froalaEditor({
              // Set the image upload URL.
              imageUploadURL: 'upload_image',
              imageUploadParams: {
                id: 'my_editor'
              }
            })
          });
        </script>

<body>
<textarea id="edit" name="content"></textarea>
</body>

@PostMapping("/upload_image") public void process(HttpServletRequest request, HttpServletResponse response) throws Exception{

    Map<Object, Object> responseData = null;

    String linkName = "http://link";

    responseData = new HashMap < Object, Object > ();
    responseData.put("link", linkName);

    // Send response data.
    String jsonResponseData = new Gson().toJson(responseData);
    response.setContentType(MediaType.APPLICATION_JSON_VALUE);
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(jsonResponseData);
}

This doesn't return JSON to Froala editor. Please help.

1
You're doing way too much work here. Ideally, use a real object instead of an untyped HashMap, but just make your process method return the object. (And if you need anything out of the request, like a parameter, just make it a method argument.) - chrylis -cautiouslyoptimistic-
What do you mean by real object? I need to to output { "link": "path/to/image.jpg" } - Lukman Wazeer
So what does it return? Check the network tab in your browser and see what response you get (status and body) - Vasan
So just return responseData. Spring does all of the translation to JSON for you. - chrylis -cautiouslyoptimistic-
Thank you much appreciated. Its working now. - Lukman Wazeer

1 Answers

1
votes

Spring MVC makes this really simple. You just need the @ResponseBody annotation on the method to indicate you're returning the body which will be serialized and sent by the framework. Since the framework does it, headers and statuses are set appropriately. Also, I've removed unused parameters and set the Map's type properly.

@PostMapping("/upload_image")
@ResponseBody 
public Map<String, String> process() throws Exception{
    Map<String, String> responseData = new HashMap<>();
    String linkName = "http://link";
    responseData.put("link", linkName);

    // Send response data.
    return responseData;
}