mercredi 15 mars 2017

Ajax doesn't work in laravel

ajax does't work in laravel

View/message.php 
<html>
<head>
  <title>Ajax Example</title>

  <script src="http://ift.tt/2eBd7UN"></script>


  <script type="application/javascript">
     function getMessage(){
        $.ajax({
           type:'POST',
           url:'/getmsg',
           data:'_token = ',         dataType: 'json',
           success:function(data){
              $("#msg").html(data.msg);
           }
        });
     }
  </script>

  <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()']);
  ?>

in above code i just click in button (Replace Message) and call ajax

Routes.php

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

AjaxController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use ajax;
use App\Http\Requests;
use App\Http\Controllers\Controller;

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

but it doesn't work it show error some like this POST http://localhost:8000/getmsg 500 (Internal Server Error)

Tutorial link



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire