Y
Y
Yaroslavv2017-05-25 19:21:21
PHP
Yaroslavv, 2017-05-25 19:21:21

Sending object to server, encoding problems (json,stringify,json_encode)?

Good day friends, I send an object to the server:
javascript

var json = [{"key":"29","value":"Country"},{"key":"30","value":"4,3,5"}];

var xhr = new XMLHttpRequest();			
xhr.open('POST', 'read.php');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');			
xhr.send('json=' + JSON.stringify(json)); //[{"key":"29","value":"Country"},{"key":"30","value":"4,3,5"}];

PHP
<?php
if (!isset($_POST['json'])) exit;
$fn = 'myjson.json';
file_put_contents($fn, json_encode($_POST['json']));

the file is written with the following value:
"[{\"key\":\"29\",\"value\":\"Country\"},{\"key\":\"30\",\"value\":\"4,3,5\"}]"

Where and what needs to be changed, what would be like in the original, without slashes (\)
Thank you in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem, 2017-05-25
@Yaroslavv

json_encode remove
why do json_encode what is already json?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question