Sending form via AJAX, method PUT

Asked

Viewed 1,939 times

0

Problem:

I am trying to send a form with ajax via PUT, but it is returning the following message on Chrome console:

PUT http://intranet.dev/%7B%7B%20URL::to('upload/'.Auth::user()-%3Eid)%20%7D%7D 403 (Forbidden) 

Script:

Follow the script in AJAX:

    $('#file').click();
    $('#file').change(function(){
        $.ajax({
            headers: {
                'X-CSRF-Token': $('input[name="_token"]').val()
            },
            type: 'PUT',
            url: "{{ URL::to('upload/'.Auth::user()->id) }}",
            data: 'file='+$('#file').val(),
            enctype: 'multipart/form-data',
            success: function(data){
                "{{ Redirect::to('/')->with('success', 'Alterado com sucesso !') }}";
            },
            error: function(){
                "{{ Redirect::to('/')->with('error', 'Erro no Ajax !') }}";
            }
        });
    });

Function:

This code causes when selecting a file to upload, it triggers my controller restful - PUT.

  • I did the test with POST and returned the same error

  • ops, files . js do not accept the Blade template. I am trying some solution and have already put here

  • If you have a folder under 'public/images' for example with the same name as the view folder, it does not work.

5 answers

1

I think the problem is you’re using the enctype: 'multipart/form-data' or in any other way informing the server that the request comes from a form. According to this response in the OS GET, POST, PUT and DELETE operations in Ajax are supported by major browsers (including the Chrome) but HTML/XHTML forms only support GET and POST requests.

1

I believe the problem lies in that line:

url: "{{ URL::to('upload/'.Auth::user()->id) }}",

If you notice the error message

PUT http://intranet.dev/%7B%7B%20URL::to('upload/'.Auth::user()-%3Eid)%20%7D%7D 403 (Forbidden)

you will see that what should be a valid URL is actually an unenterpreted piece of code. Hence error 403. For some reason, Laravel is not converting your code into a valid URL. That is, the error has nothing to do with the HTTP (PUT) method used.

  • exactly. I have found the solution friend, thank you

0

PUT? It would not recommend PUT because it is not supported by all browsers. You can send by Ajax via GET or POST. I always use GET or POST and it works well.

Documentation - The type of request to make ("POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.>

  • 2

    to use restful, it is necessary practically all methods

0


SOLUTION:

The problem was in Laravel’s Slide, my ajax page that had the name script js., was renamed to script.blade.php and after that I used the @include('script') on my main page and began to recognize the Blade command.

0

Maybe this can solve your problem:

In the JSON type attribute, you define it as POST and in your JSON date form or attribute you add a field like this:

<input name="_method" type="hidden" value="PUT">

The procedure in this case is similar to a POST request, differing only because there is a need to add an input of type Hidden specifying the type of request, which in the example shown is PUT.

  • Already found the solution friend, thank you

Browser other questions tagged

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