【发布时间】:2020-01-07 18:51:09
【问题描述】:
我有一个从 PHP 迁移到 Node.js 的应用程序。此应用程序将 javascript 对象作为数据发送到调用 PHP 处理程序的 ajax 函数。
然后,PHP 处理程序使用 PHP 的 json_encode 函数将对象转换为 JSON。 Node 有一个名为“json_encode”的包,但它与 PHP 对应的包不同。
基本上,我想转换这个:
{ 'pages[anycast][blocks][0][frames_content]': ' \n <section class="bg-white builder-bg padding-110px-tb xs-padding-60px-tb" . . . }
进入这个:
{"pages":{"anycast":{"blocks":[{"frames_content":" \n <section class=\"bg-white builder-bg padding-110px-tb xs-padding-60px-tb\" . . .}
在将其发送到服务器之前在客户端使用 JSON.stringify(data) 会创建以下内容:
{ '{"data":{"pages":{"anycast":{"blocks":[{"frames_content":" \n <section class': '\\"bg-white builder-bg padding-110px-tb xs-padding-60px-tb\\" . . . }
它添加了额外的反斜杠并错误地替换了后面的等号
客户端代码:
var pages = {}, theSite;
if( site.sitePages[0].blocks.length !== 0 ) {
for( var x = 0; x < site.sitePages.length; x++ ) {
if( site.sitePages[x].blocks.length !== 0 ) {
pages[site.sitePages[x].name] = site.sitePages[x].prepForSave();
} else {
pages[site.sitePages[x].name] = 'empty';
}
}
theSite = {
pages: pages
};
} else {
theSite = {
delete: true
};
}
//remove old alerts
$('#errorModal .modal-body > *, #successModal .modal-body > *').each(function(){
$(this).remove();
});
$.ajax({
url: 'https://api.handler.com/save',
method: 'POST',
data: theSite,
dataType: "json"
}).done(function (res) {
//enable button
$("a#savePage").removeClass('disabled');
if( res.responseCode === 0 ) {
if( showConfirmModal ) {
$('#errorModal .modal-body').html( res.responseHTML );
$('#errorModal').modal('show');
}
} else if( res.responseCode === 1 ) {
//no more pending changes
site.setPendingChanges(false);
$('body').trigger('changePage');
}
});
},
/*
preps the site data before sending it to the server
*/
prepForSave: function(template) {
this.sitePagesReadyForServer = {};
if( template ) {//saving template, only the activePage is needed
this.sitePagesReadyForServer[this.activePage.name] = this.activePage.prepForSave();
this.activePage.fullPage();
} else {//regular save
//find the pages which need to be send to the server
for( var i = 0; i < this.sitePages.length; i++ ) {
if( this.sitePages[i].status !== '' ) {
this.sitePagesReadyForServer[this.sitePages[i].name] = this.sitePages[i].prepForSave();
}
}
}
},
我通过 AJAX 将“theSite”变量发送到服务器。
原始 PHP 服务器端处理程序:
<?php
$return = [];
if( isset($_POST['data']) && $_POST['data'] != '' ) {
if( isset($_POST['data']['delete']) ) {
$myfile = fopen("site.json", "w");
fwrite($myfile, '{}');
fclose($myfile);
} else {
$myfile = fopen("site.json", "w");
fwrite($myfile, json_encode($_POST['data']));
fclose($myfile);
}
$return['responseCode'] = 1;
$return['responseHTML'] = '<h5>Hooray!</h5> <p>The site was saved successfully!</p>';
} else {
$return['responseCode'] = 0;
$return['responseHTML'] = '<h5>Ouch!</h5> <p>Something went wrong and the site could not be saved :(</p>';
}
echo json_encode($return);
?>
【问题讨论】:
-
看起来您将其序列化为字符串,而不是对象。因此,那些额外的字符。你能给我们一些代码吗?
-
在哪里生成 pages[anycast][blocks][0][frames_content] - JS 中的客户端,您正试图将其转换为 JSON 以发送到 PHP 或服务器 -在 PHP 中,你正在尝试在 JS 中阅读它?
-
显示您的原始 PHP 代码和试图替换它的 Node.js 代码。然后,我们可以向您展示您在转换中出了什么问题。
-
您的原始转换看起来像是 PHP 使用 POST 或 GET 参数完成的那种解析。我认为没有任何内置的 JS 函数可以公开这种解析,但它大概是在
requests模块内部完成的。
标签: javascript json