dtu72460 2017-04-21 09:17
浏览 140
已采纳

laravel请求将输入作为具有空值的数组键名返回

I have a form that sends a request through Ajax in laravel
This snippet from my controller returns null

use Illuminate\Http\Request;
class StatusController extends Controller {
  public function postStatus(Request $request) {
    if ($request->ajax()) {
      dd($request->input('statusBody'));  

This however returns an array with statusBody as a key name with null value

use Illuminate\Http\Request;
class StatusController extends Controller {
  public function postStatus(Request $request) {
    if ($request->ajax()) {
      dd($request->input());  

This is the returned result

array:1 [▼
  "This_is_a_sample_status" => null
]  

And this finally returns the correct value

use Illuminate\Http\Request;
class StatusController extends Controller {
  public function postStatus(Request $request) {
    if ($request->ajax()) {
      $val = $request->input('statusBody');
      dd(array_search($val, $request->all()));  

My question is: Why does this happen?
Why doesn't $request->input('name') returns a value as expected?
UPDATE:
This is also the Ajax function which successfully logs the wanted result to the console

var statusBody = $('#statusBody').val();
      $.ajax({
        type: "POST",
        url: 'status',
        data: statusBody,
        success: function(data){
          console.log(statusBody);
        }
      });
  • 写回答

1条回答 默认 最新

  • dongmaoxi0477 2017-04-21 12:15
    关注

    I tried to reproduce the problem in a fresh laravel app where I found out what's wrong.
    data parameter in the Ajax function expects a dataString like so

    var statusBody = $('#status').val();
          var dataString = "status="+statusBody;
          $.ajax({
            type: "POST",
            url: 'status',
            data: dataString
          });
    

    Thanks to Antonis to point out the Ajax call

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?