Foros de discusión

Getting Cross-Origin Request Blocked when sending request to REST

Balu Bollam, modificado hace 7 años.

Getting Cross-Origin Request Blocked when sending request to REST

New Member Mensajes: 21 Fecha de incorporación: 23/10/16 Mensajes recientes
HI Everyone,


I am Trying to access jsonWS REST url using the AJAX call...
But getting Error:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8080/api/jsonws/LoginForm-portlet.foo/hello-world/world-name/balu. (Reason: CORS header 'Access-Control-Allow-Origin' missing).


Here is My code:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).on('ready',function(){
var username ="test@liferay.com";
var password ="test";
function make_base_auth(user, password) {
var tok = user + ':' + password;
var hash = btoa(tok);
return "Basic " + hash;
}
$.ajax({
url: 'http://localhost:8080/api/jsonws/LoginForm-portlet.foo/hello-world/world-name/balaji',
dataType: "json",
type: "get",
headers: {
'Access-Control-Allow-Origin': '*'
},
success: function(data){
alert(data.employeeName);
},
beforeSend: function(xhr){
xhr.setRequestHeader('Authorization',make_base_auth(username, password));
},
complete: function(){
},
error: function(){
}
});

});
</script>

To solve This I have Tried out with below possibilities:
1.Added the following points in portlet-ext.properties file and also in setup.properties file //Still getting same Issue
json.service.auth.token.hosts.allowed=127.0.0.1,myIP
json.service.auth.token.enabled=false
jsonws.web.service.public.methods=*
jsonws.servlet.hosts.allowed=127.0.0.1,my ip

2.Added the CORS filters in Tomcat web.xml // Still not solved Getting 404 network Error
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
</init-param>
<init-param>
<param-name>cors.exposed.headers</param-name>
<param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
</init-param>
<init-param>
<param-name>cors.support.credentials</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.preflight.maxage</param-name>
<param-value>10</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


Please Anyone Help me Out..

Thanks In advance
Developer Akupintar, modificado hace 6 años.

RE: Getting Cross-Origin Request Blocked when sending request to REST

New Member Mensajes: 2 Fecha de incorporación: 13/12/17 Mensajes recientes
i am sorry to reply in this old thread.
but i have the same problem like above.

does anyone have the solution?