在Laravel 4.1中形成AJAX后响应周期。*

时间:2018-01-31 09:17:29

标签: php jquery ajax laravel laravel-4

我有一个相当旧的网站,我作为新职位的一部分继承了它 - 它是根据Laravel 4.1。*版本规范构建的。

我的问题是Response::json在响应中返回未定义的变量,使用标准的AJAX post方法,并且正确定义了所有CSRF和ajaxSetup()。

application.blade.php

 $.ajax({
   type: 'POST', //This will always be a post method for the supplier chain check form.
   url: 'supply-us/application', //URL endpoint for the post form method: we'll set this to the controller function we're targeting.
   data: { 'companyName': values['companyName'] }, //This will carry the form data that is needed to be passed to the server.
   success: function (response) {
       console.log(response['companyName']); << THIS LINE RETURNS "undefined"

       console.log(typeof response) << THIS LINE RETURNS string
   },
   error: function (response) {
       console.log(response);
   },
 }); 

values [&#39; companyName&#39;]返回我在表单中输入的内容。以上&#34;回复&#34;简单的chucks回html - 所以我认为我的路线可能在AJAX url param中被错误定义或错误定义,也许?以下是两条适用的路线:

routes.php文件

Route::controller('supply-us/application', 'ApplicationController');
Route::post('supply-us/application', 'ApplicationController@processSupplierApplication');

ApplicationController.php

<?php

use Illuminate\Http\Request;

class ApplicationController extends FrontController {
  public function getSupplierApplication() {
         return self::getPage('supply-us/application');
    }

  public function processSupplierApplication(Request $request) {
    if (Input::has('companyName')) {

       $this->companyName = Input::get('companyName');

       $data = [
          'success': true,
          'companyName': $this->companyName
       ];

       return response()->json($data);

    }

  }

}

任何专业提示将不胜感激!

1 个答案:

答案 0 :(得分:0)

在发布或获得结果时检查控制器中缺少的内容 通常我遵循

    在blade.php中
  1. <.form method="post" action="{{url('supply-us/application')}}".>
      {{csrf_field()}}
      <.input type="text" name="companyName".>
      <./form.>

  2. 删除点试试这会帮助你在控制器中找到丢失的东西

    在你的刀片中

      <.input type="text" name="companyName".>
      <./form.>
    你的ajax中的

    <.input type="text" name="companyName" id="companyName".>
    
    控制器中的

    var company = $('#companyName').val(); $.ajax({ type: 'POST', url: 'supply-us/application', data: { 'Company':company,'_token': '{{ csrf_token() }}' }, success: function (response) { alert(data) // if this not work then try this alert(data.company) }, error: function (response) { console.log(response); }, });

    }