【问题标题】:Want to use both Request::ajax() and Request to get form data想要同时使用 Request::ajax() 和 Request 来获取表单数据
【发布时间】:2018-01-25 12:26:33
【问题描述】:


在 Laravel 5.5 中,我想使用 Request::ajax() 函数,为此我必须将 use Illuminate\Http\Request 替换为 use Request
替换命名空间 ajax 函数后工作正常,但我也想获取表单参数,当我尝试使用public function ajax_form(Request $request)$request->parameter

它给了我这个错误:

未定义属性:Illuminate\Support\Facades\Request::$parameter

如何使用该 ajax 函数同时获取表单参数?

【问题讨论】:

  • 请展示完整的类方法
  • @AlexeyMezenin 你在说哪个课?控制器类?
  • 你说的表单参数是什么意思?

标签: php laravel laravel-5 laravel-5.5


【解决方案1】:

你不必替换它,Illuminate\Http\Request 也有 ajax 方法,但它不是静态的。

注入请求对象

public function index(Request $request)
{
    if ($request->ajax()) {
        return response()->json(['result' => true]);
    }

    return view('dashboard');
}

不注入请求对象

public function index()
{
    if (request()->ajax()) {
        return response()->json(['result' => true]);
    }

    return view('dashboard');
}

【讨论】:

  • 已经尝试过使用相同的,ajax 请求没有被识别!
  • 你的ajax请求怎么样?我一直在使用它
【解决方案2】:

如果您尝试使用 ajax 获取输入,那么这会有所帮助。

 public function index(Request $request)
   {
    if ($request->input('parameter')) {
      return response()->json(['result' => true]);
    }

  return view('dashboard');
 }

【讨论】:

  • 你没有得到我的问题我想验证它是简单的 POST 请求还是 AJAX 请求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-08
  • 1970-01-01
  • 1970-01-01
  • 2017-07-06
  • 1970-01-01
相关资源
最近更新 更多