1

I have a html/angular view which needs to get data from spring mvc controller which returns Json response.

Previously I used angular, getting json calling a REST url.

Not sure how to do the same when spring mvc controller returns a json response.

Thanks.

2 Answers 2

2

My sample code.

js

function sendMessage(message) {

    $.ajax({
        url : "/sample/push/" + message,
        processData : false,
        contentType : "text/html; charset=utf-8",
        type : 'POST',

        success : function(response) {
             // get response  
        },

        error : function(request, status, error) {

        },

    });
}`

controller

@RequestMapping(value = "/push/{message}")
public @ResponseBody String processResult(@PathVariable String message) {
    // "your json String"
    return pushService.pushMessage(message);
}

ajax call and spring MVC tutorial - link : this tutorial XD

Sign up to request clarification or add additional context in comments.

1 Comment

I disagree with this answer though it is a correct way to do it (but out of context), since this uses jQuery and the asker's question states that "I have a html/angular view" and this is not angular at all. Maybe the asker doesn't care about whether it's angular or not, in that case this could be fine. If so, he should change the tag angular to jquery
1

Keep in mind some concepts as partial view, angular controller, angular service and how to make async call using the $http angular service.

Basically you create a Controller (js), a Service (js) and a partial View (html)

  • In the service you implement all data logic and rest api calls
  • In the controller you manipulate data retrieved using the service and prepare it to be presented in the partials
  • In the partial you "bind" (and it is shown to the user all data, actions, etc) the info in the controller

1 Comment

From the answer, think can call $http/$q (ajax) from js to the controller path/url , thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.