1

I have a route like this --

Route::put('avote', 'voteController@avote')->middleware('auth');

I want to access this route from a ajax send request.
When i use this code --

$data = {/* some data here */};
$.post("/avote", $data, function(result) {
    $('#avote h2').html(result);
    $('#avote a span').css('color', 'orange');
    $('#avote a span').unwrap();
});

I get an error method not allowed. I know that it is the problem of method I used (used post not put)

I question is, is there any way i can get the information from /avote using ajax or any other scripts?

Please dont suggest me to change the route request from put to post or Any other way to protect the /avote route

I used Route::put() beacuse i have a database update function in the route controller

Conor
  • 2,473
  • 1
  • 14
  • 22

1 Answers1

2

Move to $.ajax() function instead of $.post and provide method (type) property:

$.ajax({
    url: "/avote", 
    data: $data, 
    method: "PUT",
    // or type: "PUT", if your jquery version is prior to 1.9
    success: function(result) {
        $('#avote h2').html(result);
        $('#avote a span').css('color', 'orange');
        $('#avote a span').unwrap();
    }
});
u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • now it is working correctly. Thanks, but now the problem is success function not working – Conor Jan 20 '17 at 12:51
  • Clarify please, what does it mean. Any errors in console? – u_mulder Jan 20 '17 at 12:52
  • no success message or anything. The request is working correctly but there is no response – Conor Jan 20 '17 at 12:52
  • See raw response, it nothing is outputted - check your route – u_mulder Jan 20 '17 at 12:53
  • May be this will help - http://stackoverflow.com/questions/797834/should-a-restful-put-operation-return-something – u_mulder Jan 20 '17 at 12:54
  • Is the request definitely getting accepted by your Laravel application? You can inspect the actual response and status code using your browser's dev tools. – edcs Jan 20 '17 at 15:55