【问题标题】:Laravel AJAX loopLaravel AJAX 循环
【发布时间】:2018-06-09 11:00:23
【问题描述】:

所以我试图将信息从表单存储到数据库中。但是,我收到的消息是 memory_limit 不断增长的循环,我真的不知道它会如何出现。

最初,我尝试更改php.ini 中的memory_limit,因为我没有看到导致问题的原因。在那之后,我看到了正在发生的事情,但从那以后我一直在努力解决它。

你怎么能解决一些你不知道它是如何工作的。

路线:

Route::resource('/office', 'OfficeController');

表格:

<form id="officeForm">
  <input type="text" name="office_name" placeholder="Има на офис..."><br>
  <input type="text" name="director" placeholder="Управител..."><br>
  <input type="text" name="address" placeholder="Адрес..."><br>
  <input type="text" name="phone_number" placeholder="Телефонен номер..."><br>
  <input type="text" name="working_time" placeholder="Работно време...">
  <button class="btn btn-primary" id="officeSubmit">Добавяне</button>
</form>

AJAX:

$('#officeSubmit').click(function(e) {
  e.preventDefault();
  $.ajaxSetup({
    headers: {
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
  });
  $.ajax({
    url: "/office",
    method: 'POST',
    data: {
      office_name: $('input[name=office_name]').val(),
      director: $('input[name=director]').val(),
      address: $('input[name=address]').val(),
      phone_number: $('input[name=phone_number]').val(),
      working_time: $('input[name=working_time]').val()
    },
    success: function() {
      $('input[name=office_name]').val('');
      $('input[name=director]').val('');
      $('input[name=address]').val('');
      $('input[name=phone_number]').val('');
      $('input[name=working_time]').val('');
    }
  });
});

控制器:

public function store(Request $request)
{
    $this->validate($request, [
        'office_name' => 'required',
        'director' => 'required',
        'address' => 'required',
        'phone_number' => 'required',
        'working_time' => 'required'
    ]);

    var_dump($request);

    $office = new Office();
    $office->office_name = $request->office_name;
    $office->director = $request->director;
    $office->address = $request->address;
    $office->phone_number = $request->phone_number;
    $office->working_time = $request->working_time;
    $office->save();

    return view('home');
}

【问题讨论】:

    标签: php mysql ajax laravel


    【解决方案1】:

    在你使用的 ajax 代码中

    method: 'POST',
    

    但是在您的路线中,您将其声明为资源。尝试将其更改为

    Router::post('/office', 'OfficeController@store');
    

    还有你没有写的表格

    <form method="POST" .....>
    

    在你使用的 store 方法的最后一行

    return view('home');
    

    做到这一点

    return redirect('/home');
    

    您还可以随时检查浏览器的网络选项卡 HTTP 请求返回的代码。是500还是其他? 200还可以。 要进入网络选项卡,请右键单击并选择检查元素,还有其他选项卡,例如元素、控制台、源,然后是网络

    【讨论】:

    • 如果您的问题得到解决,请将其标记为正确答案或投票
    猜你喜欢
    • 1970-01-01
    • 2017-01-31
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    • 2012-09-26
    • 2015-02-19
    相关资源
    最近更新 更多