dongtan3395 2017-11-23 17:32
浏览 351
已采纳

Laravel:为什么我的ajax请求返回“500(内部服务器错误)”?

I'm trying to make AJAX request with button in Laravel 5.5. Console returns me POST: 500 (Internal Server Error).

enter image description here

My Javascript code here:

<script type="text/javascript">
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    $('#insertForm').on('submit', function(e){
        e.preventDefault();
        var data = $(this).serialize();
        var url  = $(this).attr('action');
        var post = $(this).attr('method');

        $.ajax({
             type: post,
             url: url,
             data: data,
             dataType: 'json',
          success:function(data)
          {
            console.log(data)
          }
        })          
    })
</script>

My HTML form code:

<form action="{{ URL::to('add') }}" class="translation-form" method="POST" id="insertForm">
        <div class="language-area">
            <textarea class="form-control wow fadeIn" data-wow-duration="0.5s" name="words" id="lang-input" rows="10"></textarea>
        </div>
            <p class="upload-info wow fadeInDown" data-wow-duration="0.5s" data-wow-delay="0.3s">
            Add words from file
            <input type="file" name="file-1" id="file-1" class="inputfile inputfile-1" data-multiple-caption="{count} files selected" multiple />
            <label for="file-1"><span>"doc" or "docx"</span></label>
        </p>
    <div class="clearfix"></div>
    <div class="text-center translate-option">
        <button type="submit" class="btn btn-green btn-instant-translate wow fadeIn" data-wow-duration="0.5s">
            Add
        </button>
    </div>
</form>

My AjaxController:

class AjaxController extends Controller
{
    public function store(Request $request)
    {
        if($request->$ajax())
        {
            return "True request!";
        }
    }
}

And my route:

Route::post('/add', ['uses'=>'AjaxController@store', 'as'=>'ajaxAdd']);

Why does such an error occur? Ajax worked when I tried to just output the data in the console without usage requests to AjaxController.

  • 写回答

2条回答 默认 最新

      报告相同问题?

      悬赏问题

      • ¥30 VB6.0操作 webview2内核的浏览器如何精确实现网页弹窗处置
      • ¥15 pr导出的视频打不开,提示“缺少编解码器”怎么解决
      • ¥15 html里js获取php参数值不成功,帮改代码
      • ¥20 如何控制ant design的InputNumber组件 最多输入5位小数
      • ¥15 c语言学生基本信息管理系统
      • ¥100 火车头采集器采集求解
      • ¥88 关于#运行时间 时间重叠 和非重叠#的问题,如何解决?
      • ¥15 C语言,密切接触者追踪
      • ¥20 关于计算机网络问题,请附带讲解
      • ¥30 自动识别图像目标并判断