mercredi 26 octobre 2016

Laravel 5 AJAX post don't work

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 = "http://ift.tt/1yCEpkO">
          </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);
   }
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2eGX5ZL
via IFTTT

Aucun commentaire:

Enregistrer un commentaire