从Ajax请求与PHP创建JSON文件 [英] Create JSON file from Ajax request with PHP

查看:119
本文介绍了从Ajax请求与PHP创建JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多其他类似的问题都结束了,但我还没有写,从一个对象的数据(如preadsheet)到一个JSON文件。

这是我的JS:

 函数(){
    。VAR US $ p $垫= $(#SS)韦氏$ P $垫(S $ P $垫);
    VAR activeSheet = S pread.getActiveSheet();
    VAR dados = JSON.stringify(S pread.toJSON());

    activeSheet.bind($。wijmo.wijs pread.Events.EditChange,功能(发送器,参数){
        执行console.log(dados);
        $阿贾克斯({
            网址:script.php的,
            数据:dados,
            数据类型:JSON,
            键入:POST
        });
    });
}
 

的数据被发送到每当有,该文件是在服务器中创建的变化在s preadsheet但它是空的。控制台

这是运行script.php

  $ MYFILE =/file.json;
$ FH =的fopen($ MYFILE,W)或死亡(无法打开文件);
$ StringData是= $ _ POST ['数据'];
$ StringData是= json_en code($ StringData是);
FWRITE($跳频,$ StringData是);
fclose函数($ FH);
 

解决方案

我真的不知道有什么用双JSON编码脚本的作用是。我会打破到底发生了什么给你:

  VAR dados = JSON.stringify(S pread.toJSON());
 

假设取值pread.toJSON()返回一个JSON格式的字符串作为其名称所暗示的,你会最终有一个双JSON-CN codeD对象重新presentation做这个。如果的toJSON 返回一个对象,可考虑重命名功能,因为它是非常模糊的。

我们会去的premise的 dados 现在包含一个合适的JSON重新presentation的字符串。

  $。阿贾克斯({
        网址:script.php的,
        数据:dados,
        数据类型:JSON,
        键入:POST
    });
 

您要发送到的script.php (没有消息出现),和是你的第一个问题is.The AJAX 参数是错误的,因为数据包含您的数据,而不是场数据 。这样做是有意以不妨碍使用的参数,如的dataType 网​​址(这是常见的pretty的) 。 替换为:

  $。阿贾克斯({
        网址:script.php的,
        数据: {
            数据:dados
        },
        数据类型:JSON,
        键入:POST
    });
 

注意的dataType 参数回归将迫使你从你的PHP code返回一个有效的JSON,或AJAX调用会失败。

PHP

在作出这一修改, $ _ POST ['数据'] 现在将包含您的JSON-CN codeD对象字面值。唯一的修改到code是去除 json_en code 的。它已经连接codeD。你不需要任何更多。

我想你试图做的是一个请求主体传递给您的code,此时你就不会被抓住它 $ _ POST 但随着输入处理程序(的fopen(PHP://输入

I know there are lots of other similar questions all over, but I couldn't yet write the data from an object (a spreadsheet) to a JSON file.

This is my JS:

function (){
    var spread = $("#ss").wijspread("spread");
    var activeSheet = spread.getActiveSheet();
    var dados = JSON.stringify(spread.toJSON());

    activeSheet.bind($.wijmo.wijspread.Events.EditChange, function (sender, args) {
        console.log(dados);
        $.ajax({
            url: 'script.php',
            data: dados,
            dataType: "json",
            type: "POST"    
        });
    });
}

The data is sent to the console whenever there are changes in the spreadsheet, the file is created in the server but it is empty.

This is script.php

$myFile = "/file.json";
$fh = fopen($myFile, 'w') or die("impossible to open file");
$stringData = $_POST['data'];
$stringData=json_encode($stringData);
fwrite($fh, $stringData);
fclose($fh);

解决方案

I honestly don't know what the use of the double JSON encoding your script does is. I'll break down what actually happens for you:

var dados = JSON.stringify(spread.toJSON());

Assuming spread.toJSON() returns a JSON-formatted string as its name suggests, you will end up with a double JSON-encoded object representation by doing this. if toJSON returns an object, consider renaming the function, as it is highly ambiguous.

We'll go with the premise that dados now contains a proper JSON representation in a string.

   $.ajax({
        url: 'script.php',
        data: dados,
        dataType: "json",
        type: "POST"    
    });

You're sending to script.php (no news there), and THIS is where your first problem is.The ajax parameter is wrong as data contains your data, not the field data. This is done purposefully as to not obstruct access to parameters like dataType or url (which are pretty common). Replace with:

   $.ajax({
        url: 'script.php',
        data: {
            data: dados
        },
        dataType: "json",
        type: "POST"    
    });

Note that the dataType parameter return will force you to return a valid JSON from your PHP code, or the AJAX call will fail.

PHP

Having made this modification, $_POST['data'] will now contain your JSON-encoded object literal. The only modification to your code is the removal of json_encode. It is already encoded. You do not need any more of it.

I think what you were trying to do was to pass a request body to your code, at which point you would not have been catching it with $_POST but with input handlers (fopen(php://input)

这篇关于从Ajax请求与PHP创建JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆