weixin_33744854 2016-10-26 18:42 采纳率: 0%
浏览 24

Laravel 5 AJAX发布不起作用

I am new in Laravel. I try to create a ajax post function on my project. But my following code don't work. When I click the button it does not response anything but I create the function as mention on the tutorial site. I search on google but don't get any suggestion or appropriate answer. Please, anyone help me solve this problem. Any suggestion will be appreciated. My English is not good. So please, don't mind. Thank you. ohh I forget to mention that I used Laravel 5.

View file: 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>

/* route file: */

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

/* controller file: AjaxController.php */

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

2条回答 默认 最新

  • weixin_33676492 2016-10-26 19:30
    关注

    Follow this Steps:

    route.php

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

    AjaxController.php

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

    Inside View

    <html>
       <head>
          <title>Ajax Example</title>
          <meta name="_token" content="{{ csrf_token() }}" />  
    
          <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
          </script>
       </head>
    
       <body>
          <div id = 'msg'>This message will be replaced using Ajax. 
             Click the button to replace the message.</div>
    
          {{ Form::button('Replace Message',['onClick'=>'getMessage()']) }}
    
          <script>
             function getMessage(){
                var _token = $('meta[name="_token"]').attr('content');
    
                $.ajax({
                   type:'POST',
                   url:'/getmsg',
                   data: {_token: _token},
                   success:function(data){
                      $("#msg").html(data.msg);
                   },
                   error: function(data){
                        console.log(data);
                   }
                });
             }
          </script>
    
       </body>
    
    </html>
    

    Hope this help's you

    评论

报告相同问题?

悬赏问题

  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿