1

How to debug this problem ?

This is my java code. I give permission for all ip by setting Access-Control-Allow-Origin, but still it says no "Access-Control-Allow-Origin"

   return Response.ok().entity(jsonNodeToSend.toString()).header("Access-Control-Allow-Origin", "*").build();

This is my javascript code.

var dict = {
"clientMac" : "48:43:7c:53:53:d1",
"BSSID" : ""
};

$.ajax({
type: 'POST',
url: 'http://104.155.189.170:8080/Localization/rest/clientMacPosition/get/7',
crossDomain: true,
contentType: "application/json; charset=UTF-8",
data: dict,
dataType: 'json',
beforeSend: function(xhr) {
    xhr.setRequestHeader('MessageId', 'abc123');
},
success: function(responseData, textStatus, messageId) {
    console.log("success");
},
error: function(responseData, textStatus, errorThrown) {
    console.log(textStatus);
    console.log(responseData);
    console.log(errorThrown);
}

});

Here is my method which gets the data and respond to the request.

@Path("/get/{customerProjectId}")
@POST
@Produces({ MediaType.APPLICATION_JSON })
public Response gettingRecentPosition(LocationAPIReceiverDTO locationAPIReceiverDTO,
                                      @PathParam("customerProjectId") int customerProjectId) {

     JsonNode jsonNodeToSend = null;


    return Response.ok().entity(jsonNodeToSend.toString()).header("Access-Control-Allow-Origin", "*").build();


}

URL: http://104.155.189.170:8080/Localization/rest/clientMacPosition/get/7

jsonData : { "clientMac" : "48:43:7c:53:53:d1", "BSSID" : "33:" }

My REST call response in POST man.

enter image description here

vidhya sagar
  • 87
  • 1
  • 10
  • Imho setting only the header is not enough. You need code that handles the preflight request that is specified here: https://www.w3.org/TR/cors/ . In node i use a package for that. I'm sure in JAVA is a similar solution. Perhaps: http://software.dzhuvinov.com/cors-filter.html ? – Thomas Aug 09 '17 at 12:25
  • Did you able to hit your API with postman – Ajit Soman Aug 09 '17 at 12:53
  • @AjitSoman yes I can do that, but it does not work in ajax. – vidhya sagar Aug 09 '17 at 13:15
  • @AjitSoman It does work in PostMan. – vidhya sagar Aug 09 '17 at 13:23
  • Could you provide URL , Header and data that you are sending – Ajit Soman Aug 09 '17 at 13:29
  • @AjitSoman I have provided the needed data. – vidhya sagar Aug 09 '17 at 13:38
  • Did you able to get data with this : `$.ajax({ type: 'POST', url:'http://104.155.189.170:8080/Localization/rest/clientMacPosition/get/7', contentType: "application/json;", data: { "clientMac" : "48:43:7c:53:53:d1", "BSSID" : "33:" }, success: function(response) { console.log(response); }, error: function(error) { console.log(error); }` – Ajit Soman Aug 09 '17 at 13:43
  • @AjitSoman I have added this, but still the same problem. You can try in jsfiddle or in your local. – vidhya sagar Aug 09 '17 at 13:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/151505/discussion-between-vidhya-sagar-and-ajit-soman). – vidhya sagar Aug 09 '17 at 14:17
  • Did you tried all solutions available at this url:https://stackoverflow.com/questions/23450494/how-to-enable-cross-domain-requests-on-jax-rs-web-services – Ajit Soman Aug 09 '17 at 17:45
  • @AjitSoman I have tried all solutions. I even added FilterHolder to the embedded jetty server, but still, i have the same problem. Still at office. GET works, only POST doesnt works. – vidhya sagar Aug 09 '17 at 17:50
  • Let us continue this discussion in chat. – Ajit Soman Aug 09 '17 at 17:51
  • Please try this : $.post("http://104.155.189.170:8080/Localization/rest/clientMac‌​Position/get/7", { "clientMac" : "48:43:7c:53:53:d1", "BSSID" : "33:" }, function(data, status){ console.log("Data: " + data + "\nStatus: " + status); }); – Ajit Soman Aug 09 '17 at 17:58

2 Answers2

1

Please create a filter for your application and add the following header to fix CORS issue:

public class ApplicationFilter implements Filter {

 @Override
 public void destroy() {
     // TODO Auto-generated method stub

 }

 @Override
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
 throws IOException, ServletException {

     HttpServletRequest httpRequest = (HttpServletRequest) request;
     HttpServletResponse httpResponse = (HttpServletResponse) response;
     httpResponse.setHeader("Access-Control-Allow-Origin", httpRequest.getHeader("Origin"));
     httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
     httpResponse.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE,PUT");
     httpResponse.setHeader("Access-Control-Max-Age", "3600");
     httpResponse.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization, Content-Type,*");
     if (httpRequest.getMethod().equals("OPTIONS")) {
         httpResponse.setStatus(HttpServletResponse.SC_OK);
         return;
     }
     chain.doFilter(httpRequest, httpResponse);
 }


 @Override
 public void init(FilterConfig arg0) throws ServletException {
     // TODO Auto-generated method stub

 }

}

Dipen Chawla
  • 331
  • 2
  • 10
Ajit Soman
  • 3,926
  • 3
  • 22
  • 41
0

Have you tried to put @CrossOrigin annotation on your controller method?

Here is an example of how to set up cross origin with spring: https://spring.io/guides/gs/rest-service-cors/

Ruslan Akhundov
  • 2,178
  • 4
  • 20
  • 38
  • I am not using any spring. I have just deployed jar file with the internal jetty server in vm. Added the javax.ws.rs.* framework. Everything working perfectly for GET method, not for POST. – vidhya sagar Aug 09 '17 at 12:39
  • ok, can you post controller class? Or at least the whole method declaration where you are responding to a request – Ruslan Akhundov Aug 09 '17 at 12:42
  • I have done it. Please take a look. Do i need to add some other annotation? I have no controller class. I have only REST class. – vidhya sagar Aug 09 '17 at 13:20
  • have you tried this: https://stackoverflow.com/questions/23450494/how-to-enable-cross-domain-requests-on-jax-rs-web-services – Ruslan Akhundov Aug 09 '17 at 13:28