【问题标题】:Ajax doesn't work in laravelAjax 在 laravel 中不起作用
【发布时间】:2017-03-15 07:29:10
【问题描述】:

ajax 在 laravel 中不起作用

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

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>


  <script type="application/javascript">
     function getMessage(){
        $.ajax({
           type:'POST',
           url:'/getmsg',
           data:'_token = {{ csrf_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()']);
  ?>

在上面的代码中,我只需单击按钮(替换消息)并调用 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);
}
}

但它不起作用 它显示一些像这样的错误 POST http://localhost:8000/getmsg 500(内部服务器错误)

Tutorial link

【问题讨论】:

  • 请向我们提供您的错误信息。
  • POST localhost:8000/getmsg 500(内部服务器错误)在控制台中
  • 错误信息,而不是错误代码
  • “加载资源失败:服务器响应状态为 500(内部服务器错误)”在控制台中
  • 你应该看看你的 nginx 日志

标签: php laravel-5.1


【解决方案1】:

只需更改代码即可解决... 数据:{'_token' : '{{ csrf_token() }}'}

【讨论】:

    猜你喜欢
    • 2015-11-19
    • 1970-01-01
    • 2018-05-16
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 2015-04-17
    • 2015-09-08
    • 1970-01-01
    相关资源
    最近更新 更多