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 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退