Laravel AJAX把方法405错误

时间:2016-09-07 21:13:45

标签: php ajax laravel-5.2 laravel-routing

我正在尝试通过AJAX put方法发送数据。我不知道我做错了什么。

所有代码可用代码 link

我的路线档案

Route::resource('restaurant', 'RestaurantController');

RestaurantController

public function update(Request $request, $id)
{

     $rules = array (
         'address_id' => 'required|alpha',
         'name' => 'required|alpha',
         'description' => 'required',
         'phone' => 'required',
         'email' => 'required|email',
         'homemade' => 'required' 
 );
 $validator = Validator::make ( Input::all (), $rules );
 if ($validator->fails ())
     return Response::json ( array (             
             'errors' => $validator->getMessageBag ()->toArray () 
     ) );
 else {
    $items = Restaurant::find ($id);
     $items->address_id = ($request->address_id);
     $items->name = ($request->name);
     $items->description = ($request->description);
    $items->phone = ($request->phone);
     $items->email = ($request->email);
     $items->homemade = ($request->homemade);
     $items->save ();
     return response ()->json ( $items );
     }
}

ajax请求

 $('.modal-footer').on('click', '.edit', function() {
    $.ajax({
        type: 'PUT',
        url: '/restaurant',
        data: {
            '_token': $('input[name=_token]').val(),
            'id': $("#fid").val(),
            'address_id': $('#address_id').val(),
            'name': $('#name').val(),
            'description': $('#description').val(),
            'phone': $('#phone').val(),
            'email': $('#email').val(),
            'homemade': $('#homemade').val(),
            'lat': $('#lat').val(),
            'lng': $('#lng').val(),
            'bank_name': $('#bank_name').val(),
            'bank_code': $('#bank_code').val(),
            'paypal_email': $('#paypal_email').val(),
            'paypal_merchantname': $('#paypal_merchantname').val(),
            'zipcode': $('#zipcode').val(),
            'easypeisa': $('#easypeisa').val(),
            'cod': $('#cod').val()

        },
        success: function(data) {
            if (data.errors){
                $('#myModal').modal('show');
               if(data.errors.address_id) {
                    $('.address_id_error').removeClass('hidden');
                     $('.address_id_error').text("address_id name can't be empty !");
                 }
                 if(data.errors.name) {
                    $('.name_error').removeClass('hidden');
                     $('.name_error').text("name can't be empty !");
                 }
                 if(data.errors.description) {
                    $('.description_error').removeClass('hidden');
                     $('.description_error').text("description must be a valid one !");
                 }

0 个答案:

没有答案