2016-03-05 60 views
3

問候和尊重我希望在數據庫表中記錄要更新的記錄時,但不幸的是,我不做任何無法獲取ID並注入AJAX和AJAX的任何內容,我想要控制器和我從數據庫和表中讀取的數量之間的關係。如何從控制器獲取記錄到Ajax laravel 5.2

我的控制器:

public function index() 
{ 
    $category = Category::all(); 
    return view('admin.category', compact('category')); 
} 
public function indexAjax(messageRequest $request){ 
    $id=$request->id; 
    $category=Category::where('id',$id)->first(); 
    $data=array('data'=>$category); 
    return $data; 
} 
public function create(messageRequest $request) 
{ 
    try { 
     Category::create($request->all()); 
     return response()->json(array('sms'=>'save')); 
    }catch (Exception $e){ 
     return response()->json(array('err'=>'error')); 
    } 
} 

我的路線:

​​

腳本:

  <script> 
      $('#submit').on('click', function (e) { 
       e.preventDefault(); 
       var data = $('#create').serialize(); 
       $.ajax({ 
        type: 'post', 
        url: '{!! URL::route('category') !!}', 
        data: data, 
        success: function (data) { 
         alert(data.sms); 
        }, 
        error: function() { 
         alert(data.err); 
        } 
       }); 
       $.ajax({ 
        type: 'GET', 
        url: '{!! URL::route('categoryAjax') !!}', 
        async: false, 
        dataType: 'json', 
        data: data, 
        success: function (data) { 
         alert(data.data); 
        } 
       }); 
      }); 
      $.ajaxSetup({ 
       headers: { 
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') 
       } 
      }); 
     </script> 

我改變alert(data.data)alert('hi)但不運行:( 改變:

    $.ajax({ 
        type: 'GET', 
        url: '{!! URL::route('categoryAjax') !!}', 
        async: false, 
        dataType: 'json', 
        data: data, 
        success: function (data) { 
         alert('hi'); 
        } 
       }); 

我的Ajax方法POST:

    $.ajax({ 
        type: 'post', 
        url: '{!! URL::route('category') !!}', 
        data: data, 
        success: function (data) { 
         alert(data.sms); 
        }, 
        error: function() { 
         alert(data.err); 
        } 
       }); 

我的控制器POST方法:

public function create(messageRequest $request) 
{ 
    try { 
     Category::create($request->all()); 
     return response()->json(array('sms'=>'save')); 
    }catch (Exception $e){ 
     return response()->json(array('err'=>'error')); 
    } 
} 

我的路線POST方法:

Route::group(['middleware' => 'web'], function() { 
Route::auth(); 
Route::post('category', ['as' => 'category', 'uses' => '[email protected]e']); 
}); 
+0

你定義了Route :: get('category',['uses'=>'categoryController @ index']) 爲什麼你讓ajax類型爲'post'?這完全沒有意義。 –

+0

Post方法發送數據並接收連接到控制器被添加了時間。試試 –

回答

0

這段代碼我的問題是Solved.But從我的腳本並運行刪除$.get用POST方法。

public function create(messageRequest $request) 
{ 
    try { 
     $category = Category::create($request->all())->id; 
     return response()->json(['id'=>$category]); 
    }catch (Exception $e){ 
     return response()->json(array('err'=>'error')); 
    } 
} 

通過此代碼返回警報中的記錄ID。

2

在Laravel 5.2,中間件具有網絡,api和auth。 如果您想使用ajax發送的數據,您將使用帶令牌的api中間件。 你可以看到這個參考:

首先,api_token添加到用戶:

首先認爲你需要做的是一個api_token列添加到你的用戶表。如果您剛剛啓動應用程序,則可能會因修改Laravel附帶的用戶遷移而將您的新列納入其中。

// add this to your users_table migration 
$table->string('api_token', 60)->unique(); 

控制器:

public function upload() 
{ 
    return view('uploads.upload', ['api_token' => Auth::user()->api_token]); 
} 

查看:

<script> 
    $(function() { 
     'use strict'; 

     var url = '/api/photos?api_token={{ $api_token }}'; 

     // Initialize the jQuery File Upload widget: 
     $('#fileupload').fileupload({ 
      // Uncomment the following to send cross-domain cookies: 
      // xhrFields: {withCredentials: true}, 
      url: url 
     });  
</script> 

路線:

Route::group(['middleware' => ['auth:api']], function() { 
    Route::post('api/photos', '[email protected]'); 
}); 
+0

感謝您的好意不會麻煩其他代碼? –

+0

我不確定。但是你可以修改路線並嘗試。 O______O – WeiYuan

+0

msg有沒有錯誤? – WeiYuan

相關問題