Ale*_*s G 16 php json shared-hosting
我json_decode在PHP中用来解析POST中的数据(POST本身是通过ajax执行的).我的代码看起来像这样:
$data = json_decode($_POST['logdata'], true);
if(!$data) {
$ecodes = array(
JSON_ERROR_NONE => "No error has occurred",
JSON_ERROR_DEPTH => "The maximum stack depth has been exceeded",
JSON_ERROR_STATE_MISMATCH => "Invalid or malformed JSON",
JSON_ERROR_CTRL_CHAR => "Control character error, possibly incorrectly encoded",
JSON_ERROR_SYNTAX => "Syntax error",
JSON_ERROR_UTF8 => "Malformed UTF-8 characters, possibly incorrectly encoded"
);
$err = json_last_error();
$result = array("error" => 1, "error_msg" => "Invalid log data: " . $ecodes[json_last_error()]);
echo json_encode($result);
exit;
}
... //use $data array
Run Code Online (Sandbox Code Playgroud)
这段代码工作正常.在我的本地linux机器或我的mac上执行时,传递的数据被正确解码,一切正常.但是,当在godaddy共享主机上执行相同的代码时,解码将失败并出现语法错误.已经花了很多时间试图缩小问题,我发现,当我有问题发生<或>在JSON对象属性值的字符.
javascript中的上传部分(ajax)如下所示:
$.ajax({
url: '/emaillog.php',
type: 'POST',
dataType: 'json',
data: { logdata: JSON.stringify(this.logData[this.scenarioLogId]) },
cache: false,
error: function(jqXHR, textStatus, errorThrown) {
alert("Error occurred during upload: " + textStatus);
},
success: function(data, textStatus, jqXHR) {
var msg = data.error
? "An error occurred processing action log: " + data.error_msg
: "Action log processed successfully.";
alert(msg);
}
})
Run Code Online (Sandbox Code Playgroud)
如您所见,POST参数的值是使用的,JSON.stringify并且在检查时(客户端和服务器端)都是正确的.如果重要,这是提交的POST参数的示例值:
{
"scenarioId":"1",
"scenarioName":"MOH",
"startTime":1355496349,
"log":[
{
"role":"Leader",
"task":"MOH",
"response":"start",
"time":1355496349
},
{
"role":"Head",
"task":"<span class=\"bold\">Assign Role</span>",
"response":"done",
"time":1355496351
}
]
}
Run Code Online (Sandbox Code Playgroud)
如果删除了数组中的最后一个元素(具有任务分配角色),则会成功处理所有内容.如果我用空格替换<和>>字符,一切都很好.
由此产生两个问题:
如果这样的JSON字符串上的语法错误是正确的行为,那么为什么我没有在其他两个环境中看到错误?另外,为什么会JSON.stringify产生错误的字符串呢?
如果这样的JSON字符串上的语法错误是不正确的行为,那么godaddy的PHP实现或配置必定是错误的.
请注意,虽然我明确询问了godaddy,但它只是相关的,因为它是代码产生错误的唯一提供者.有人可以对这种情况有所了解吗?
Ale*_*s G 14
无论出于何种原因,仅在godaddy托管(也许在其他一些地方,但在我测试的其他地方没有),当我提交表单时,服务器正在逃避引用 - 这就是导致问题的原因.问题的解决方案非常简单:而不是做
$data = json_decode($_POST['logdata'], true);
Run Code Online (Sandbox Code Playgroud)
我做到了
$data = json_decode(stripslashes($_POST['logdata']), true);
Run Code Online (Sandbox Code Playgroud)
一切都很完美.