Battousai
Battousai

Reputation: 513

jQuery ajax post in CakePhp 3.x not able to get data in controller

I have looked everywhere and for some reason I cannot get the data to go to my controller when posting via 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 );
        });

I get this in console when stringifying:

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

I have tried to send the data back as response from controller:

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;
}

Upvotes: 1

Views: 3677

Answers (2)

user6613363
user6613363

Reputation:

You can try this

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;
}

Upvotes: 0

Wes King
Wes King

Reputation: 627

jQuery handles urlencoding the object passed into 'data' in $.ajax() for you. This should do the trick:

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

or if you're 100% keen on passing JSON as your request body, you can decode the data using the input method on the request:

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;
}

I would read more into the JSON and XML Views Docs for CakePHP 3.

Upvotes: 1

Related Questions