You can simply use the json_encode()
function to return JSON response from a PHP script. Also, if you’re passing JSON data to a JavaScript program, make sure set the Content-Type
header.
Let’s take a look at an example to understand how it basically works:
$project_id = $row["id"]; $slug = $row["slug"]; $data = ['project_id' => $project_id, 'slug' => $slug]; echo json_encode( $data );
$.ajax({ url:'https://forlancer.in/php/app/update_project_status.php', method:'POST', data:{freelancer_id: freelancer_id}, success:function(data){ let Data = JSON.parse(data); // string to object convertion console.log(Data.project_id); // window.location.href="https://forlancer.in/manage-project"; } });