weixin_33736649 2016-10-07 07:47 采纳率: 0%
浏览 36

Laravel 5中没有Ajax响应

I am trying to get an Ajax response using Laravel 5 but it just wont work. This is the error I see in the Chrome debugger:

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

This is message.php in resources/views:

<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>

This is my 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);


}
}

Then I added this to the web.php in routes:

Route::get('ajax',function(){
   return view('message');
});
Route::post('/getmsg','AjaxController@index');
  • 写回答

4条回答 默认 最新

  • weixin_33714884 2016-10-07 07:50
    关注

    Change the url in this ajax code to the correct path (e.g //public/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>
    
    评论
  • YaoRaoLov 2016-10-07 07:58
    关注

    You can set CSRF token once for all your calls:

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

    And you can return array in your action and it will be automaticly converted to json:

    public function index(){
          $msg = "This is a simple message.";
          return ['msg'=> $msg];
    }
    
    评论
  • 谁还没个明天 2016-10-07 08:05
    关注

    change in ajax call if your url in 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>
    
    评论
  • weixin_33690963 2016-10-07 08:05
    关注

    I did it by setting the ajax type to GET:

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

    And the route set to get instead of post:

    Route::get('/getmsg','AjaxController@index');
    

    This works but I have absolutely no idea why the POST method doesn't at all. I think the appropiate solution for this would be to know why POST doesn't work so please, even though this one works too, if someone knows how to do it with POST just tell.

    评论

报告相同问题?

悬赏问题

  • ¥15 存储过程或函数中的结果集类型变量如何使用。
  • ¥80 关于海信电视聚好看安装应用的问题
  • ¥15 vue引入sdk后的回调问题
  • ¥15 求一个智能家居控制的代码
  • ¥15 ad软件 pcb布线pcb规则约束编辑器where the object matpcb布线pcb规则约束编辑器where the object matchs怎么没有+15v只有no net
  • ¥15 虚拟机vmnet8 nat模式可以ping通主机,主机也能ping通虚拟机,但是vmnet8一直未识别怎么解决,其次诊断结果就是默认网关不可用
  • ¥20 求各位能用我能理解的话回答超级简单的一些问题
  • ¥15 yolov5双目识别输出坐标代码报错
  • ¥15 这个代码有什么语法错误
  • ¥15 给予STM32按键中断与串口通信