Form submission via AJAX, method PUT

0

Problem:

I am trying to send a form with ajax via PUT, but it is returning the following message in the 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 .

    
asked by anonymous 30.01.2014 / 13:42

5 answers

0

SOLUTION:

The problem was in the Laravel blade, my ajax page that was named script.js , was renamed to script.blade.php and after that I used the @include ('script') on my home page and came to recognize the blade command.

    
30.01.2014 / 14:46
1

I think the problem lies in this 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 uninterpreted piece of code. So the 403 error. For some reason, Laravel is not converting your code to a valid URL. That is, the error has nothing to do with the HTTP (PUT) method used.

    
30.01.2014 / 14:49
1

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

    
30.01.2014 / 14:21
0

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

  

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

    
30.01.2014 / 13:45
0

Maybe this can solve your problem:

In the JSON type attribute, you define it as POST and in your form or JSON date attribute adds 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.

    
30.01.2014 / 15:00