PHP: file_get_contents('php://input') returning string for JSON message

前端 未结 7 1485
梦毁少年i
梦毁少年i 2020-12-19 04:26

I am trying to read in a JSON message in my PHP app and this is my php code:

$json = file_get_contents(\'php://input\');
$obj = json_decode($json, TRUE);
ech         


        
7条回答
  •  孤城傲影
    2020-12-19 04:29

    If you are using javascript to send JSON, to a php file

    send_recieve.js

    var myObject = JSON.stringify({"name":"John","age":30,"city":"New York"});
    var xhr = new XMLHttpRequest();
    xhr.open("POST","http://localhost/dashboard/iwplab/j-comp/receive_send.php",false);
    xhr.setRequestHeader("Content-type","application/json");
    xhr.onreadystatechange = function(){
        if(xhr.readyState==4){console.log("xhr response:"+xhr.response)}
        alert(xhr.responseText);
     };
     xhr.send(myObject);
    

    recieve_send.php

    
    

    echo statements work as a response.

提交回复
热议问题