简体   繁体   English

使用ajax PHP将json数据保存到json文件

[英]saving json data to json file using ajax PHP

My json file looks like this: 我的json文件如下所示:

count_click.json count_click.json

[
  {
    "link": "google.com",
    "count": 2  
  },
  {
    "link": "yahoo.com",
    "count": 3
  }
]

now I open this file using 现在我使用打开文件

$.getJSON('count_click.json',function(data){

    // do something with data

   var stringData = JSON.stringify(data);

    $.ajax({
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    url: 'http://127.0.0.x:3xx9/update.php',
    data: {stringData: stringData},
    success : function(d){
       alert('done');}            
    })

}) // end of getJSON function 

update.php update.php

<?php
$a = file_get_contents("php://input");
file_put_contents('http://127.0.0.x:3xx9/count_click.json', json_encode($a));
?>

I get error in the browser console: 我在浏览器控制台中收到错误:

POST http://127.0.0.x:3xx9/update.php 404 (Not Found)

But the file is there. 但是文件在那里。 When I go to this http://127.0.0.x:3xx9/update.php in the browser, I see the contents of the php fine perfectly fine. 当我在浏览器中转到http://127.0.0.x:3xx9 / update.php时,我看到php的内容完全正常。

You could edit your PHP: 您可以编辑您的PHP:

<?php
    $a = $_POST['stringData'];
    // you should check $a consists valid json - what you want it to be
    file_put_contents('count_click.json', $a);

You really should check that posted data to be valid and not saving something unwanted. 您确实应该检查发布的数据是否有效,并且不要保存不必要的内容。 Also you could check that request really is POST -> $_SERVER['REQUEST_METHOD'] . 您也可以检查请求是否确实是POST > $_SERVER['REQUEST_METHOD']

Maybe you find some other methods to improve security (for example only allow post from own domain...). 也许您发现了其他提高安全性的方法(例如,仅允许来自自己域的帖子...)。

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! 同样, file_put_contents需要磁盘上实际文件的路径,而不是URL!

data: {stringData: stringData} from your AJAX request means you can access it on your server with $data = $_POST['stringData']; 您的AJAX请求中的data: {stringData: stringData}表示您可以通过$data = $_POST['stringData'];在服务器上访问它$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). 如果这不起作用,请尝试使用浏览器访问端点(不是文件,因为浏览器不需要PHP即可读取它)。

Point your browser to http://127.0.0.x:3xx9/update.php and on your server, simply 将浏览器指向http://127.0.0.x:3xx9/update.php并在服务器上,只需

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. 如果您不这样做,那么这将超出JS和PHP,可能与服务器的设置有关。 If you are using RStudio's Shiny Server , then that does not work for PHP 如果您使用的是RStudio的Shiny Server ,则不适用于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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM