user2810718 user2810718 - 10 months ago 40
Ajax Question

cannot set return value in ajax call

I am new to web technologies and trying to build up through examples .This question seems to be recurring. However, I am unable to get the correct syntax/method.

I am attempting to read the value set by an ajax call (action.php) and use it on client side (js). Below is my attempt

type: "POST",
url: "/crm/action.php",
data: form,
success: function(data){
error: function () {
myalert("DATABASE ERROR","X Record not saved. if this error persists, contact your system administrator");

And action.php simply tries to set the variable "error" as below

$error = 1;

All my attempts have returned "undefined" so far.

Answer Source

change your action.php to the following:

die(json_encode(['error' => 1]));

Make sure there isn't anything else outputted but the above.