weixin_33696822 2016-07-12 02:36 采纳率: 0%
浏览 18

Ajax Laravel 5.2不起作用

I want to develop simple ajax with laravel5.2 with this code

This oneline_help.php view

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

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/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 the routes

Route::get('/ajax','front@support');
Route::post('/getmsg','Hello@index');

This is front @support

public function support() 
{
    return view('online_help', array('title' => 'Welcome', 'description' => '', 'page' => 'online_help','subscribe'=>"",'brands' => $this->brands));
}

This is Hallo @index controller

public function index(){
       echo"i in in hello index";
      $msg = "This is a simple message.";
      return response()->json(array('msg'=> $msg), 200);
   }

The button appear But when click on it, The text doesn't change . Please tell me why and how to resolve it.

  • 写回答

1条回答 默认 最新

  • lrony* 2016-07-12 06:59
    关注

    Here is my working example of AJAX....

    You don't need to add token in your ajax request, make it global so with every ajax request, your CSRF token will be added automatically.

    In your HTML Head section add this meta tag

    <meta name="csrf-token" content="<?php echo csrf_token() ?>">
    

    Then add this code in your Javascript tags. this will add CSRF token in every ajax request.

    Note this required jquery file should be included in your page. once include call this method below from the file.

    <script type="text/javascript">
        var csrf_token   =   $('meta[name="csrf-token"]').attr('content');
        $.ajaxSetup({
           headers: {"X-CSRF-TOKEN": csrf_token}
        });
    </script>
    

    I have modified your method....

      <script>
         function getMessage(){
            $.ajax({
               type:'POST',
               url:'/getmsg',
               data:'_token = <?php echo csrf_token() ?>', //remove this line
               dataType:'json', // you skipped this line
               beforeSend:function(){
                  alert('loading....'); //this will show loading alert
               },
               success:function(data){
                  $("#msg").html(data.msg);
               },
               error:function(){
                  alert('loading error...')
               }
            });
         }
      </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料