简体   繁体   English

Laravel 5 AJAX发布不起作用

[英]Laravel 5 AJAX post don't work

I am new in Laravel. 我是Laravel新手。 I try to create a ajax post function on my project. 我尝试在我的项目上创建一个ajax发布函数。 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. 我在Google上搜索,但没有任何建议或适当的答案。 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. 哦,我忘了提到我使用过Laravel 5。

View file: message.php 查看文件: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);
   }
}

For easy use, you can tell jQuery to always pass through the current csrf token. 为了易于使用,您可以告诉jQuery始终传递当前的csrf令牌。 You can achieve this in two steps: 您可以分两步实现:

Add meta tag in your head 在您的头部添加元标记

<meta name="csrf-token" content="{{ csrf_token() }}">

Setup your ajax 设置你的ajax

$.ajaxSetup({
  headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  }
});

After these steps, there is no need to include the token in the individual ajax requests. 完成这些步骤后,无需在单个ajax请求中包括令牌。

Note: the docs explain this very clearly. 注意: 文档对此进行了非常清晰的解释。

Follow this Steps: 请按照以下步骤操作:

route.php route.php

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

AjaxController.php AjaxController.php

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

Inside View 内部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 希望这对你有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM