0

I am trying to send response back to ajax request in js by using REST API.

send request from ajax to server is okay and its processing okay. But to send the response from server to ajax, the ajax receive it as "error" instead of "success".

My ajax in js

enter image description here

My REST API

enter image description here

I have tried wp_send_json, json_encode, wp_send_json_success, but results are still the same.

This is what I have in Error from ajax enter image description here

1 Answer 1

0

On line 252 you are using print_r() inside your code, which is printing out an error message before the proper response, so your response is not valid JSON anymore.

If, as it appears, you want to include the output of print_r() in your error log, and not include it in the response, you need to set the second argument so that the value of print_r() is returned, and not printed:

error_log( "get_detail =" . print_r( $data, true ) );
1
  • thank you. did not expect that to be the problem.
    – murni
    Commented Apr 22, 2021 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.