D
D
Dima Sokolov2019-04-07 11:51:20
PHP
Dima Sokolov, 2019-04-07 11:51:20

Convert http response from php to NodeJS?

<?php  
$target_dir = "uploads/";  
$target_file_name = $target_dir .basename($_FILES["file"]["name"]);  
$response = array();  
  
// Check if image file is an actual image or fake image  
if (isset($_FILES["file"]))   
{  
   if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file_name))   
   {  
     $success = true;  
     $message = "Successfully Uploaded";  
   }  
   else   
   {  
      $success = false;  
      $message = "Error while uploading";  
   }  
}  
else   
{  
      $success = false;  
      $message = "Required Field Missing";  
}  
$response["success"] = $success;  
$response["message"] = $message;  
echo json_encode($response);  
  
?>

How will $response look like in JavaScript syntax?
I think something like this, but something does not work
res.write(JSON.stringify({
          success: true,
          message: 'Successfully Uploaded'
        })

Or maybe the problem is in the client side:
com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question