java - Cross-Origin Request Blocked Spring REST service + AJAX -


unable call spring rest service

my spring service

@requestmapping(value = "/mas/authenticate", method = requestmethod.post) public responseentity<map<string, string>> authenticate(@requestbody subject subject) {     map<string, string> result = new hashmap<string, string>();     result.put("result_detail", "invalid password");     result.put("result", "failure");     httpheaders responseheaders = new httpheaders();     responseheaders.setcontenttype(mediatype.application_json);     responseheaders.add("access-control-allow-origin", "*"); // added header allow cross domain request domain     return new responseentity<map<string, string>>(result, responseheaders, httpstatus.ok); } 

my ajax code

$.ajax( {   crossdomain: true,   type: "post",   contenttype: "application/json; charset=utf-8",   async: false,   url: "http://localhost:8080/springmvc/rest/mas/authenticate",   headers: {"access-control-allow-origin" : "*"},   data:{},   datatype: "json", //also tried "jsonp"   success: function(data, status, jqxhr)   {     alert('success');   },   error: function(jqxhr, status)   {     alert('error');   } }); 

i getting following error :(

cross-origin request blocked: same origin policy disallows reading remote resource @ http://localhost:8080/springmvc/rest/mas/authenticate. can fixed moving resource same domain or enabling cors.

i have tried datatype: "jsonp". append body object url make different url , cannot hit service url , got 404 error.

my browser: firefox 36.0.4

how can rid error, help?

my ajax call , service ok. after searching lot on internet have found server side problem not client side.

on server side spring have implement filter allow cors requests.

filter this.

import java.io.ioexception;  import javax.servlet.filterchain; import javax.servlet.servletexception; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse;  import org.apache.commons.logging.log; import org.apache.commons.logging.logfactory; import org.springframework.web.filter.onceperrequestfilter;  public class corsfilter extends onceperrequestfilter {     private static final log log = logfactory.getlog(corsfilter.class);      @override     protected void dofilterinternal(httpservletrequest request, httpservletresponse response, filterchain filterchain) throws servletexception, ioexception {          response.addheader("access-control-allow-origin", "*");         if (request.getheader("access-control-request-method") != null && "options".equals(request.getmethod())) {             log.trace("sending header....");             // cors "pre-flight" request             response.addheader("access-control-allow-methods", "get, post, put, delete");             // response.addheader("access-control-allow-headers", "authorization");             response.addheader("access-control-allow-headers", "content-type");             response.addheader("access-control-max-age", "1");         }         filterchain.dofilter(request, response);     }  } 

and in web.xml apply filter on service requests this

    <filter>         <filter-name>cors</filter-name>         <filter-class>com.test.common.controller.corsfilter</filter-class> <!-- package name , filter class -->     </filter>     <filter-mapping>         <filter-name>cors</filter-name>         <url-pattern>/*</url-pattern>     </filter-mapping>  

this may else went through problem. :)


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -