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

报告相同问题?