Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何让Ajax将JSON发送到PHP脚本_Javascript_Php_Ajax_Json - Fatal编程技术网

Javascript 如何让Ajax将JSON发送到PHP脚本

Javascript 如何让Ajax将JSON发送到PHP脚本,javascript,php,ajax,json,Javascript,Php,Ajax,Json,我正在制作一个需要将数据存储在服务器上的.json文件中的网页 以下是我迄今为止所尝试的: Javascript代码: // variable j = our json var j; function loadDoc(){ var xmlhttp; if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest();

我正在制作一个需要将数据存储在服务器上的.json文件中的网页

以下是我迄今为止所尝试的:

Javascript代码:

// variable j = our json
var j;
function loadDoc(){
    var xmlhttp;
    if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else{// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            j = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","things.json",true);
    xmlhttp.send();
}
loadDoc();
function rewrite(){
    var xhr;  
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...  
        xhr = new XMLHttpRequest();  
    } else if (window.ActiveXObject) { // IE 8 and older  
        xhr = new ActiveXObject("Microsoft.XMLHTTP");  
    }  
    xhr.onreadystatechange = function(){
        if(xhr.readyState == 4){

        };
    };
    xhr.open("POST", "write.php", true);
    xhr.send("data=" + j);
};
<?php
$data = $_POST['data'];  
file_put_contents('things.json', $data);
?>
PHP文件:

// variable j = our json
var j;
function loadDoc(){
    var xmlhttp;
    if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else{// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            j = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","things.json",true);
    xmlhttp.send();
}
loadDoc();
function rewrite(){
    var xhr;  
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...  
        xhr = new XMLHttpRequest();  
    } else if (window.ActiveXObject) { // IE 8 and older  
        xhr = new ActiveXObject("Microsoft.XMLHTTP");  
    }  
    xhr.onreadystatechange = function(){
        if(xhr.readyState == 4){

        };
    };
    xhr.open("POST", "write.php", true);
    xhr.send("data=" + j);
};
<?php
$data = $_POST['data'];  
file_put_contents('things.json', $data);
?>

注意,在代码的其他部分中,
j
变量已更改

我的问题是,在PHP脚本将JSON文件变为空白之后。我做错什么了吗?php是否正确接收JSON?如果是,我如何解决这个问题?

干杯


如果你投了反对票,请告诉我原因。

要像HTML表单一样发布数据,请使用setRequestHeader()添加HTTP标头。() 因此,它必须是:
setRequestHeader(“内容类型”、“应用程序/json”)

您如何/在何处调用
rewrite()
函数?您是否确定您的异步
loadDoc()
函数在此之前已完成?您在重写文件时是否检查了
j
的值?这是一个全局文件,因此您必须小心…@jeroen首先声明了
loadDoc
函数。@gfish3000
j
的值已经建立并正确无误。请尝试将输出写入其他文件中-尽管它可能也是空的。从逻辑上讲,这意味着write.php脚本无法使用数据。所以它要么丢失了,要么从未去过那里。我能想到的潜在原因是:对于你的文章大小来说太大(检查日志),或者$u文章没有被填充,或者可能在重定向过程中丢失了值或者其他什么。我现在真的想不出更多的原因,但是你可能可以从这里查找一些东西……但是一定要检查访问日志,看看事情是否按顺序和方式发生。