0

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);
3
  • Why are you trying to json_encode() the already-encoded data? Commented Oct 15, 2013 at 10:50
  • shouldn't you json_decode the incomming data? Commented Oct 15, 2013 at 10:50
  • I tried json_decode($stringData); as well but the result is the same: an empty JSON file Commented Oct 15, 2013 at 10:56

2 Answers 2

1

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)

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, thanks this was helpful! It is now creating the file although it has lots of backslashes in it, like: {\"activeSheetIndex\":0,\"sheetCount\":1,\"tabStripRatio\":0.5,\"tabStripVisible\":true,\"tabEditable\":true,\"newTabVisible\":true,\"referenceStyle\":0,\"useWijmoTheme\":false,\"canUserEditFormula\":true,\"startSheetIndex\":0,\"allowUndo\":true,\
@NunoNogueira: disable magic quotes or upgrade to a good PHP version, or just stripslashes() from the input.
Yes, stripslashes() did it. Thanks for your help!
0

That's great. I was having this "\" issue and i was struggling to resolve this. Thanks for your comments stripslashes() resolved the same issue for me as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.