php - Wordpress rest api, update info

557

I have custom post 'note' and the route to update the content of the particular note:

register_rest_route( 'university/v1', 'note/(?P<id>\d+)', array(
    'methods' => 'POST',
    'callback' => 'uni1UpdateNote',
) );


function uni1UpdateNote($data){
    return $data['title'];   // $data['title'] returns null, $data returns {}
}

and my js file:

    let data = {
        'title': noteBlock.find('.note-title').html(),
        'content': noteBlock.find('.note-content').html(),
    };

    $.ajax({
        beforeSend: (xhr) => {
            xhr.setRequestHeader('X-WP-Nonce', uni1NoteData.nonce)
        },
        url: uni1NoteData.urlRoot+'/wp-json/university/v1/note/'+getID(),
        type: 'POST',
        data: data,
        success: function(response) {
            console.log(response);
        },
        error: function(response){
            console.log(response);
        }
    });

As I understand the route's callback function gets parameters $data from javascript file. How to check what is in $data variable? I use postman, but I get null or empty object. What am I doing wrong? I just want to update my note.

People are also looking for solutions to the problem: php - laravel: login on front page? Should I use two controllers?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.