Request Cors for webservice

Asked

Viewed 246 times

3

I am trying to submit a form via POST to a WebService but I’m having the following problem:

No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'null' is therefore not allowed access. The Response had HTTP status code 404.

I’ve already added CORS filters to the project with WebServices.

This is my requisition:

$("#frmLogin").submit(function(e){
    var postData = $(this).serializeArray();
    var fUrl = $(this).attr("action");

    $.ajax({
        type: 'POST',
        url: fUrl,
        contentType: 'application/x-www-form-urlencoded',
        xhrFields:{
            withCredentials: false
        },
        crossDomain : true,
        xhrFields: {
            withCredentials: false
        },
        data: postData,
        success: function(result){
            alert('sucesso');
        },
        error: function(){
            alert('erro');
        }

    });
});

Something’s missing from it?

  • Are you submitting to the same domain or another? in the second case you are sure the service accepts POSTexternal s?

  • the request is being made from a different domain. In the case of the service I configured this way: I added some settings to the web.xml file

  • Namely: <filter> <filter-name>CORS</filter-name> <filter-class>com.thetransactioncompany.Cors.Corsfilter</filter-class> <init-param> <param-name>Cors.allowGenericHttpRequests</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>Cors.allowOrigin</param-name> <param-value>*</param-value>... etc

1 answer

2

In your $.ajax add the option

headers: { 'Access-Control-Allow-Origin': '*' },
  • I’ll try this way. Thank you.

  • It is also possible to add to which specific domain you want to release.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.