Domanda

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

È stato utile?

Soluzione

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 ) );
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a wordpress.stackexchange
scroll top