Can anyone help me what is wrong in this code ajax using with php? [closed]

alert(output); shows the success message if the data is inserted.

It should show [object Object].

You are looking at a string, and not an object parsed from JSON.

Since it is a string, it doesn’t have a status property.

const output="{ "status": "Succsess" }";
alert(output);
alert(output.status);

You need to tell jQuery that the response is JSON so it will parse it into an object.

header("Content-Type: application/json");
echo json_encode($result);

Note that you must set the headers before sending any other output.

Without setting the Content-Type header explicitly, PHP will default to claiming that the JSON is HTML … which it isn’t.

Note that the below is for the sake of example. You should not add JSON.parse to your client-side code. jQuery will do that behind the scenes.

const output = JSON.parse('{ "status": "Succsess" }');
alert(output);
alert(output.status);

Leave a Comment