I have the following JS script:
var data = {
action: 'my_action',
sku: SKU
};
$.ajax({
type:"POST",
url:myObject.url,
data: data,
dataType: 'json',
success: function (response) {
console.log(response);
},
error: function(jqXHR, textStatus, errorThrown)
{
alert(errorThrown);
}
});
Here I am calling a PHP function my_action. In PHP I construct the function and the callback properly, and have these two functions:
function test_function()
{
return "Test";
}
function my_action_callback() {
$response = test_function();
echo json_encode($response);
die();
}
This causes an error inside the alert: "Unexpected end of input".
However, should the callback look like this:
function my_action_callback() {
$response = "Test";
echo json_encode($response);
die();
}
I get "Test" returned in JS.
Why doesn't the first snippet work?
Edit
In fact, the error seems to stem from me calling the my_test_function in PHP. The following PHP code also returns the unexpected end of input error:
function test_function()
{
return "Test";
}
function my_action_callback() {
$response = test_function();
$response = "Test";
echo json_encode($response);
die();
}