Laravel 4.1.* AJAX 响应方式

Posted

技术标签:

【中文标题】Laravel 4.1.* AJAX 响应方式【英文标题】:Laravel 4.1.* AJAX response method 【发布时间】:2018-07-10 15:32:48 【问题描述】:

我正在尝试使用 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 "未定义"

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

【问题讨论】:

@Tschallacka Enlightening - 我应该应该使用什么? Input::get 是您想要使用的。它与请求的类型无关,只是您从输入中获取字段。如果你使用console.log(response),你会得到什么?如果你得到了 json 字符串,那么你需要在你的 javascript 中对其进行解码,然后才能得到属性。 您的帖子被有权删除的部分删除是有原因的。它可以被视为非常被动的攻击性,也被社区视为绒毛。问题不需要它,*** 不需要它。 @aynber 这实际上返回了我要发布到的页面的 html ...。减去很多。似乎只是页眉和页脚? 有趣的是它给了你 html 而不是实际的响应。这意味着它正在引发某种错误。尝试在 ajax URL 的开头添加一个斜杠,这样它就不会因为位于“子目录”中而发生变化。检查您在app/storage/logs 中的日志以查看是否有任何内容被抛出,并检查开发者控制台的网络选项卡中的标题以确保它没有返回 404 或 500。 【参考方案1】:

更新函数参数如下;

<?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);

    
  

【讨论】:

传递给 ApplicationController::processSupplierApplication() 的参数 1 必须是 Illuminate\Http\Request 的实例,没有给出

以上是关于Laravel 4.1.* AJAX 响应方式的主要内容,如果未能解决你的问题,请参考以下文章

如何响应ajax搜索输出查看~laravel

AJAX 发送数组作为 Laravel 的响应

Ajax 在 Laravel 中只获得成功响应

在 jQuery $.ajax 中读取 Laravel JSON 响应

Laravel 在 Ajax 响应中返回一个视图

Laravel - 检查 Ajax 是不是请求