2016-10-07 47 views
1

我試圖使用Laravel 5來獲得Ajax響應,但它只是不會工作。這是錯誤我在Chrome調試器看到:在Laravel 5中沒有Ajax響應

POST http://localhost:8000/getmsg 500 (Internal Server Error)send @ jquery.min.js:4ajax @ jquery.min.js:4getMessage @ ajax:10onclick @ ajax:25 

這是message.php資源/瀏覽次數:

<html> 
    <head> 
     <title>Ajax Example</title> 

     <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"> 
     </script> 

     <script> 
     function getMessage(){ 
      $.ajax({ 
       type:'POST', 
       url:'/getmsg', 
       data:'_token = <?php echo csrf_token() ?>', 
       success:function(data){ 
        $("#msg").html(data.msg); 
       } 
      }); 
     } 
     </script> 
    </head> 

    <body> 
     <div id = 'msg'>This message will be replaced using Ajax. 
     Click the button to replace the message.</div> 
     <?php 
     echo Form::button('Replace Message',['onClick'=>'getMessage()']); 
     ?> 
    </body> 

</html> 

這是我Ajaxcontroller.php:

<?php 
namespace App\Http\Controllers; 
use Illuminate\Http\Request; 
use App\Http\Requests; 
use App\Http\Controllers\Controller; 

class AjaxController extends Controller { 
    public function index(){ 
     $msg = "This is a simple message."; 
     return response()->json(array('msg'=> $msg), 200); 


} 
} 

然後我在路由中加入了web.php:

Route::get('ajax',function(){ 
    return view('message'); 
}); 
Route::post('/getmsg','[email protected]'); 
+0

嘗試在JS添加此:$ .ajaxPrefilter(功能(選項,originalOptions,XHR){VAR 記號= $( '元[NAME = 「csrf_token」]')ATTR( '內容'); 如果。 (令牌)返回xhr.setRequestHeader('X-XSRF-TOKEN',令牌); } });並嘗試使用命名路線。編輯:這實際上不起作用,因爲你沒有在元中添加令牌。嘗試使用主視圖,在其中添加對所有頁面都通用的所有內容,然後對其進行擴展。 Laracast有一些很棒的教程 – Indra

回答

1

可以適用於所有電話設置CSRF令牌一次:

<script> 
    const xCsrfToken = "{{ csrf_token() }}"; 
    $.ajaxSetup({ 
     headers: { 
      'X-CSRF-TOKEN': xCsrfToken 
     } 
    }); 
</script> 

而且你可以返回數組在您的行動,將全自動轉換成JSON:在Ajax調用

​​
1

更改URL在這個Ajax代碼,以正確的路徑(例如//公共/的getMsg)

<script> 
     function getMessage(){ 
      $.ajax({ 
       type:'POST', 
       url:'/<project_name/public/getmsg', 
       data:'_token = <?php echo csrf_token() ?>', 
       success:function(data){ 
        $("#msg").html(data.msg); 
       } 
      }); 
     } 
     </script> 
0

變化如果你的htttp://hostname/getmsg

網址
<script> 
     function getMessage(){ 
      $.ajax({ 
       type:'POST', 
       url:'<?php echo url('/') ?>/getmsg', 
       data:'_token = <?php echo csrf_token() ?>', 
       success:function(data){ 
        $("#msg").html(data.msg); 
       } 
      }); 
     } 
     </script> 
0

我做到了通過設置AJAX類型爲GET:

<script> 
     function getMessage(){ 
      $.ajax({ 
       type:'GET', 
       url:'/getmsg', 
       data:'_token = <?php echo csrf_token() ?>', 
       success:function(data){ 


$("#msg").html(data.msg); 

      } 
     }); 
    } 
</script> 

,並設置爲獲得的,而不是職位的路線:

Route::get('/getmsg','[email protected]'); 

這工作,但我絕對不知道爲什麼POST方法根本沒有。我認爲適當的解決方案是知道爲什麼POST不起作用,所以請即使這也起作用,如果有人知道如何用POST做到這一點,只要告訴。

+1

你有沒有試過我給過的答案? – Ronald

+0

@Ronald Yep。完全相同的問題。 – prgrm

+1

你可以設置你的路線只是'getmsg'?並按照我給出的答案 – Ronald