简单地说,我可以使用下面的代码将任何结果发布为一个数组,消息格式为json,例如:
array('result'=>"{'code':'101', 'message':'ok!!'}")
对于我的用户来说,这种格式的结果不是标准的,他们只喜欢以json格式获得结果,例如:
{'code':'101', 'message':'ok!!'}
我找不到其他代码来代替我的代码来返回结果
我的代码:
function redirect_to_customer($result = [], $redirect_url = "")
{
$html = '<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body onload="closethisasap();">
<form name="redirectpost" method="POST" action="' . $redirect_url . '">
';
if (!is_null($result)) {
$result = json_encode($result,true);
$html .= '<input type="hidden" name="result" value="' . str_replace('"', "'", $result) . '"> ';
}
$html .= "
</form>
</body>
<script type='"text/javascript'">
function closethisasap() {
document.forms['"redirectpost'"].submit();
}
</script>
</html>";
echo $html;
}
您可以通过在字段名称中使用括号以html形式发布数组:
<form action="" method="post">
<input type="hidden" name="result[]" value="'code':'101'" />
<input type="hidden" name="result[]" value="'message':'ok!!'" />
<input type="submit" value="submit" />
</form>
将输出:
array(1)
["result"]=>
array(2) {
[0]=>
string(12) "'code':'101'"
[1]=>
string(16) "'message':'ok!!'"
}
}