0

I have a REST api endpoint like this -

@RequestMapping(value = "/createform/custom/name", method = RequestMethod.POST)
public String nameSubmit(@RequestBody String name) {
    return "you have submitted this name ***** "+name;
}

From angular service I tried to make a REST call like this -

var data = 'name='+inputName;
$http.post(uploadUrl, data, {
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).then(
        function(success){
            alert(success.data);
        },
        function(error){
            alert(error.status);
        }
     );

Now I always get a -1 HTTP status and control goes to error block. I also tried @RequestParam instead of @RequestBody but no luck. But if I try to access the service through curl or chrome postman , everything works fine. Only when I try through angular application I get stuck with -1 response.

bowserm
  • 1,046
  • 10
  • 25
  • what is the error exactly? – Sal-laS Feb 06 '17 at 18:40
  • Are you able to post to a rest controller that doesn't require any arguments? I'd start at that baseline and then once you can post something completely basic, see if you can post that name. What have you already tried? – bowserm Feb 06 '17 at 18:51
  • Just now I tested with removing parameter and POST method, so my controller method is like this now, @RequestMapping(value = "/createform/custom/name") public String nameSubmit() { return "success"; }. Again from postman it ran fine but from application it's still landing into -1 error block. This is a new controller having only this method, and this is the first time I am trying to access this .... -1 is associated with timeout error, so is this some CORS related issue ? If yes then how can I set it ? –  Feb 06 '17 at 19:27

1 Answers1

0

It was a CORS issue. I tried the Spring proposed solutions first. But the "gs-rest-service-cors" jar was not found in Maven repository. Hence ended up writing a filter as shown here and things worked fine.