saving json data to json file using ajax PHP

前端 未结 2 1362
伪装坚强ぢ
伪装坚强ぢ 2021-01-17 02:45

My json file looks like this:

count_click.json

[
  {
    \"link\": \"google.com\",
    \"count\": 2  
  },
  {
    \"link\": \"yah         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-17 03:17

    A few problems.

    file_get_contents("php://input"); Why? You are already sending a Post with data, no need to complicate things with a stream.

    Also file_put_contents needs the path to the actual file on disk, not a URL!

    data: {stringData: stringData} from your AJAX request means you can access it on your server with $data = $_POST['stringData'];.

    Simply echo something out to see if you are actually getting anything.

    echo json_encode( array("Payload" => $_POST['stringData']) );

    If that doesn't work, try accessing the endpoint with your browser (not the file as that does not need PHP for the browser to read it).

    Point your browser to http://127.0.0.x:3xx9/update.php and on your server, simply

    echo "Request received!";

    If you see that in your browser, your endpoint is working and you can continue troubleshooting. If you don't, then this is beyond JS and PHP and probably has to do with your server's settings. If you are using RStudio's Shiny Server, then that does not work for PHP

    In any case, your endpoint should always return something when called. Not just save the file. It is just good practice.

    header("HTTP/1.1 200 OK");

提交回复
热议问题