Laravel 4.1。* AJAX响应方法

时间:2018-01-31 13:42:15

标签: php jquery ajax laravel laravel-4

我尝试使用AJAX设置一个简单的POST方法,发布到Laravel控制器并进行处理。

我遇到的问题是返回AJAX调用理解并可以使用的响应。

routes.php文件

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

获取表单数据的AJAX:

   $('#supplierChainCheckForm').submit(function( event ) {
     event.preventDefault();

     function csrfSafeMethod(method) {
         return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
     }

     // As we're using the "csfrUnsafeMethod" of POST - we'll need to setup the csfr token to be passed between client and server:
     $.ajaxSetup({
         // This is standard before send method for the ajaxSetup() function:
         beforeSend: function(xhr, settings) {
             // If settings.type in $.ajax method is unsafe i.e., if it is 'POST' then we'll need to set X-CSRFToken in the xhr Request Header: omitted && sameOrigin(settings.url) currently;
             if (!csrfSafeMethod(settings.type)) {
                 xhr.setRequestHeader("X-CSRFToken", $('meta[name="csrf-token"]').attr('content'));
             }
         }
     });

     // Get all the form inputs into an array:
     var $inputs = $('#supplierChainCheckForm :input');
     // We can now loop over all of the input names & values of the form:
     var values = {};
     $inputs.each(function() {
         values[this.name] = $(this).val();
     });

     $.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'] ,'_token': '{{ csrf_token() }}'}
     }).done(function(response) {
       console.log(response.companyName);
     });
   });

ApplicationController.php

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;

class ApplicationController extends FrontController {

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

  public function processSupplierApplication() {

    if (!Input::get('companyName') == null) {
      $company = Input::get('companyName');

      return Response::json([ 'companyName' => $company ], 200);
    } else {
      $company = "No compnay specified";

      return Response::json([ 'companyName' => $company ], 200);

    }
  }
}

然而,结合以上所有内容给我

console.log(response.companyName) as&#34; undefined &#34;

请指教。请注意,我使用 Laravel 4.1。*

1 个答案:

答案 0 :(得分:0)

更新功能参数如下;

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Input;

class ApplicationController extends FrontController {

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

  public function processSupplierApplication(Request $request) {

    if (!$request->input('companyName') == null) {
      $company = $request->input('companyName');

      return Response::json([ 'companyName' => $company ], 200);
    } else {
      $company = "No compnay specified";

      return Response::json([ 'companyName' => $company ], 200);

    }
  }
}