I have this:
add_action( 'rest_api_init', function () {
register_rest_route('my-project/v1/form', '/post', array(
'methods' => 'POST',
'callback' => 'post_form'
) );
});
I called it with this and the call gets done and serializedForm is not empty:
$(".my-project-submit").click(function(){
var serializedForm = $('#my-project-form').serialize();
$.post("/wp-json/my-project/v1/form/post/", serializedForm, function(data) {
alert(data);
});
});
I tried these:
function post_form($data) {
$data is null/empty.
function post_form($data) {
$data = $request->get_params();
I don't know it $data is null, I just do not receive response ( alert(data)
never reached).
function post_form($data) {
$data = $request->get_body();
I don't know it $data is null, I just do not receive response ( alert(data)
never reached).
All test methods end with
$response = new WP_REST_Response($data, 200);
$response->set_headers([ 'Cache-Control' => 'must-revalidate, no-cache, no-store, private' ]);
return $response;
}
I have this:
add_action( 'rest_api_init', function () {
register_rest_route('my-project/v1/form', '/post', array(
'methods' => 'POST',
'callback' => 'post_form'
) );
});
I called it with this and the call gets done and serializedForm is not empty:
$(".my-project-submit").click(function(){
var serializedForm = $('#my-project-form').serialize();
$.post("/wp-json/my-project/v1/form/post/", serializedForm, function(data) {
alert(data);
});
});
I tried these:
function post_form($data) {
$data is null/empty.
function post_form($data) {
$data = $request->get_params();
I don't know it $data is null, I just do not receive response ( alert(data)
never reached).
function post_form($data) {
$data = $request->get_body();
I don't know it $data is null, I just do not receive response ( alert(data)
never reached).
All test methods end with
$response = new WP_REST_Response($data, 200);
$response->set_headers([ 'Cache-Control' => 'must-revalidate, no-cache, no-store, private' ]);
return $response;
}
Share
Improve this question
edited Apr 15, 2019 at 8:52
TTT
asked Apr 12, 2019 at 15:15
TTTTTT
3291 gold badge4 silver badges17 bronze badges
11
|
Show 6 more comments
1 Answer
Reset to default 4Update your function post_form
as
function post_form(WP_REST_Request $request)
{
$data = $request->get_params();
print_r($data);
}
$request
is undefined in your examples so it will never work, the parameter passed to the callback is the request object, but you've named yours$data
for some reason, not$request
– Tom J Nowell ♦ Commented Apr 12, 2019 at 15:28serialize
generates a URL encoded string, it's not standard JS practice when usingPOST
, and would require additional steps on the PHP end to deserialize – Tom J Nowell ♦ Commented Apr 12, 2019 at 15:30serialize
in jQuery here. For reference, PHP should be giving you warning and notice messages in your error log for that code due to the use of an undefined variable, so check you've got your debugging setup right and PHP error logs are working – Tom J Nowell ♦ Commented Apr 12, 2019 at 15:31