【问题标题】:jQuery ajax post in CakePhp 3.x not able to get data in controllerCakePhp 3.x 中的 jQuery ajax 帖子无法在控制器中获取数据
【发布时间】:2015-12-22 05:34:02
【问题描述】:

我到处寻找,但由于某种原因,通过 ajax 发布时我无法将数据发送到我的控制器。

jQuery:

        var newDate = {};
        newDate['start'] = startyear+"-"+startmonth+"-"+startday+" "+starthour+":"+startminute+":00";
        newDate['end'] = endyear+"-"+endmonth+"-"+endday+" "+endhour+":"+endminute+":00";
        newDate['allday'] = allday;
        console.log(JSON.stringify(newDate));
        var url = plgFcRoot + "events/update/"+event.id;
        $.ajax({
            type: 'POST',
            url: url,
            data: JSON.stringify(newDate),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
        })
        .done( function( data ) {
            console.log( data );
        })
        .fail(function( data ) {
            console.log( data );
        });

字符串化时我在控制台中得到这个:

{"start":"2015-12-7 21:30:00","end":"2015-12-7 22:30:00","allday":0}

我已尝试将数据作为控制器的响应发回:

public function update($id = null)
{
    $event = $this->Events->get($id);
    $event = $this->Events->patchEntity($event, $this->request->data);
    $this->Events->save($event);
    $this->set(compact('event'));
    $this->response->body($this->request->data());
    return $this->response;
}

【问题讨论】:

  • 那么你的问题到底是什么?你试过调试$this->request->data吗?
  • 数据似乎没有发送到控制器。我尝试在 done 回调中显示来自 ajax 的响应,但我得到的只是空 json,它在 responseText 中只显示“array”。
  • @arilia debug 给了我数组到字符串的转换...然后我 json_encode($this->request->data) 仍然得到一个空数组。
  • 那么你是如何解决这个问题的?我还是有问题

标签: javascript jquery ajax cakephp cakephp-3.x


【解决方案1】:

jQuery 会为您处理传递到 $.ajax() 中“数据”的对象的 urlencoding。这应该可以解决问题:

var newDate = {};
    newDate['start'] = startyear+"-"+startmonth+"-"+startday+" "+starthour+":"+startminute+":00";
    newDate['end'] = endyear+"-"+endmonth+"-"+endday+" "+endhour+":"+endminute+":00";
    newDate['allday'] = allday;
    console.log(JSON.stringify(newDate));
    var url = plgFcRoot + "events/update/"+event.id;
    $.ajax({
        type: 'POST',
        url: url,
        // NOTICE JSON.stringify() has been removed!
        data: newDate
    })
    .done( function( data ) {
        console.log( data );
    })
    .fail(function( data ) {
        console.log( data );
    });

或者如果您 100% 热衷于将 JSON 作为请求正文传递,您可以使用请求中的输入法解码数据:

public function update($id = null)
{
    $event = $this->Events->get($id);
    $request_data = $this->request->input('json_decode');
    $event = $this->Events->patchEntity($event, $request_data);
    $this->Events->save($event);
    $this->set(compact('event'));
    $this->response->body(json_encode($request_data));
    return $this->response;
}

我会阅读更多关于 CakePHP 3 的 JSON and XML Views Docs

【讨论】:

  • 我也有同样的问题,不用JSON.stringify
【解决方案2】:

你可以试试这个

public function update($id = null)
{
    $event = $this->Events->get($id);
    $data = json_decode($this->request->data);
    $event = $this->Events->patchEntity($event, $data);
    $this->Events->save($event);
    $this->set(compact('event'));
    $this->response->body($this->request->data);
    return $this->response;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    • 2020-09-24
    • 2017-02-12
    相关资源
    最近更新 更多