作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 JavaScript 中有一个数组,它是 grossIncome
。这个数组包含 6 个元素。现在我想使用 $.ajax
方法将此数组发送到另一个 PHP 页面,但我如何才能将此数组编码为 JSON 格式?
最佳答案
使用 JSON.stringify()
(如果你想支持旧的浏览器,你必须包含来自 Douglas Crockford 的 this file):
$.ajax(
{
"url": "some_script.php",
"data": {json: JSON.stringify(gossIncome)},
"success": function()
{
// Do something!
}
});
在 PHP 脚本中,您可以使用 json_decode
对其进行解码:
<?php
$json = isset($_GET['json']) ? $_GET['json'] : exit('No JSON passed!');
$array = json_decode($json);
if ( json_last_error() != JSON_ERROR_NONE )
exit('JSON invalid!');
?>
关于php - 如何将数组编码为 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8066219/
我是一名优秀的程序员,十分优秀!