Ajax REST: PUT/DELETE does not work

Asked

Viewed 426 times

0

I have 2 applications:

1) a Rest API on an XPTO server (Using Laravel 5.1), which has:

  ->header('Access-Control-Allow-Origin', '*.xpto.com.br')
  ->header('Access-Control-Allow-Credentials', 'true')
  ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE')
  ->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Content-Range, Content-Disposition, Content-Description');

2) In my Client: Static Front that makes AJAX GET/POST/PUT/DELETE for API above.

GET/POST happens normal, but when I do a PUT/DELETE it returns:

"Response to preflight request doesn’t pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested Resource"

Which doesn’t make much sense, because if I didn’t have Origin, neither would the POST.

Method I use:

var objXPTO = function() {
    ...
    this.Ajax = function(url,type,args,callback) {
            //console.log(url,type,args,callback);
            $.ajax({
                 url: url,
                 method: type,
                 data: args
                }).done(function(data) {
                 callback(data);
            });
        }
    ...

}

Below has some light?

  • puts _method: 'PUT' as argument and test the call.

  • @bgsouza, worked with the help of the above comment?

  • Hello @Ruggi and Marco I tested like this and did not roll My code was right, I saw today that the problem was the CSRF of Laravel 5.1 Thanks for the help

  • So @bgsouza answer your own question and answer it, so someone who has the same problem you know how to handle the situation.

  • @Ruggi decsulpe but the usability of this here is horrible

1 answer

1

Guys I managed to solve my problem, actually it was Laravel’s CSRF that was blocking me I decided to implement a token :)

Browser other questions tagged

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