1

I am trying to do a simple call to a servlet in ajax :

$.ajax({
    type: 'GET',
    url: 'http://myserver/c/myfunction',
    cache : false,
    data: {key: 'value'},
    success:function(){
        alert("success");
    },
    error:function(){
        alert("error");
    }
});

with my servlet function in java :

@RequestMapping(value = "/c/myfunction", method = {RequestMethod.GET})
public synchronized void myfunction(HttpServletRequest request, HttpServletResponse response)  {
    response.setStatus(HttpServletResponse.SC_OK);
}

I get a status code 200 OK, but I still see the "error" alert. Any suggestion?

Arcyno
  • 4,153
  • 3
  • 34
  • 52
  • 2
    Did you check your browser console? Also, you're not sending back anything, which is probably what causes the error. –  Jan 16 '19 at 14:01
  • It seems to happen because I use two different servers : "has been blocked by CORS policy". How should I solve that in my case ? – Arcyno Jan 16 '19 at 14:05
  • Allow Cors headers in your backend. – Adis Azhar Jan 16 '19 at 14:16

1 Answers1

1

Found the answer to my question by simply looking at my console... It said request has been blocked by CORS policy.

So I had to add response.addHeader("Access-Control-Allow-Origin", "*"); to my java code to make it work.

Arcyno
  • 4,153
  • 3
  • 34
  • 52