正确返回具有嵌入式 JS 函数的 JSON 对象
Properly returning a JSON object with an embedded JS function
更新帖子
到目前为止,我有一个适用于我的问题的解决方案(仅在 Firefox 上测试过)。我的主要目标是用Ajax动态更新Flot图。我还需要动态更新轴样式,您可以通过勾报格式化程序选项执行此操作。为了避免我以前遇到的问题,我只需在 PHP 数组中插入所需的返回字符串并对该数组进行 JSON 编码。返回 Ajax 函数后,我使用 javascript Function() 构造函数 (Link) 创建了一个新函数。我希望这对某人有所帮助。
阿贾克斯
$.ajax({
url: 'someControllerWhichCreatesJSONdata',
data: 'some Value',
type: 'post',
cache: false,
datatype: 'json'
})
.success(function(data) {
var datas = data[0]; // fetch data array
var options = data[1]; // fetch options array
var xReturn = options.xaxes[0].tickFormatter; // fetch return string
var yReturn = options.yaxes[0].tickFormatter;
var xFunc = new Function("val", "axis", xReturn); // create function
var yFunc = new Function("val", "axis", yReturn);
options.xaxes[0].tickFormatter = xFunc; // update tickFormatter with function
options.yaxes[0].tickFormatter = yFunc;
$.plot($("#yw0"), datas, options);
})
.error(function(data) {
$("#error").html(data.responseText); // show error in separate div
});
PHP数组
$options = array(
'legend' => array(
'position' => 'nw',
'show' => true,
'margin' => 10,
'backgroundOpacity' => 0.5
),
'grid' => array(
'clickable' => true,
'hoverable' => true
),
'pan' => array('interactive' => true),
'zoom' => array('interactive' => true),
'axisLabels' => array('show' => true),
'xaxes' => array(
array(
'axisLabel' => 'someUnit',
'tickFormatter' => 'return "foo bar"' // enter whatever return value you need
)
),
'yaxes' => array(
array(
'axisLabel' => 'someUnit',
'tickFormatter' => 'return "foo bar"'
)
)
);
我跳过数据数组,因为它看起来很相似。两个数组合并并 JSON 编码。
public function actionSomeControllerWhichCreatesJSONdata() {
$returnArray = array($data, $options);
echo json_encode($returnArray);
return true;
}
生成的数组看起来像我在这篇文章底部发布的数组。
原始帖子
我现在正在尝试几个小时,但我无法让它工作。
我有一个 Ajax 请求,它在成功时获取一个 JSON 对象作为返回值。只要我不在我的 JSON 数组中使用 JS 函数,这就可以正常工作。所以我的 JSON 数组如下所示(json_encode 年后):
[{
"data": [{
{1,2},
{3,4},
}],
"function": "function(){return '"foo bar'";}"
}]
为了摆脱函数字符串中的"'s。我使用str_replace并将带引号的字符串替换为不带引号的字符串。这看起来像这样:
[{
"data": [{
{1,2},
{3,4},
}],
"function": function(){return "foo bar";}
}]
普通json_encode工作正常,我的 Ajax 函数将其读取为 JSON 对象。替换字符串后,结果发现返回值不再是 JSON 对象,而是纯文本字符串。我尝试使用 jquery.parseJSON() 再次解析字符串,但这会导致语法错误:
语法错误:JSON.parse:JSON 数据第 1 行第 396 列处的意外关键字
Ajax 函数:
$.ajax({
url: 'someControllerWhichCreatesJSONdata',
data: 'some Value',
type: 'post',
cache: false,
datatype: 'json' // or text when trying to parse
})
.success(function(data) {
console.log(data);
var dat = jQuery.parseJSON(data); // trying to parse (only when dataType: "text")
var datas = dat[0]; // or data[0] when datType = json
var options = dat[1]; // ---
$.plot($("#yw0"), datas, options); // trying to update a FLOT window
})
.error(function(data) {
console.log("error");
$("#error").html(data.responseText); // show error in separate div
});
因此,当使用此函数并将返回类型设置为"json"时,会产生错误。如果返回类型是"text"并且我尝试解析字符串,则会出现上述错误。这个问题有什么解决方案吗,还是我做错了什么?
感谢您的帮助!
更新抱歉,我的 JSON 数据当然无效!正如我所说,我的 JSON 对象是由json_encode创建的,我想它应该得到正确的语法。son_encode后面的普通数组如下所示:
[[{
"data":[[0.0042612,0.0042612]],
"label":"WISE.W3: Cutri et. al 2012",
"lines":{"show":false},
"points":{"show":true,"radius":3}}],
{
"legend": {
"position":"nw",
"show":true,
"margin":10,
"backgroundOpacity":0.5},
"grid": {
"clickable":true,
"hoverable":true},
"pan":{"interactive":true},
"zoom":{"interactive":true},
"axisLabels":{"show":true},
"axisLabel": {
"unit":"Jy",
"id":null,
"name":null},
"tickFormatter":"$tickFormatter$",
"position":"right"
}]
str_replace后我得到
[[{
"data":[[0.0042612,0.0042612]],
"label":"WISE.W3: Cutri et. al 2012",
"lines":{"show":false},
"points":{"show":true,"radius":3}}],
{
"legend": {
"position":"nw",
"show":true,
"margin":10,
"backgroundOpacity":0.5},
"grid":{"clickable":true,"hoverable":true},
"pan":{"interactive":true},
"zoom":{"interactive":true},
"axisLabels":{"show":true},
"axisLabel":{"unit":"Jy","id":null,"name":null},
"tickFormatter":function(val, axis){return val.toExponential(2)},
"position":"right"
}]
Adeno 已经指出,您的 JSON 语法无效。请尝试使用 linter 验证您的 JSON,例如 http://jsonformatter.curiousconcept.com/
这要好一点:
[{"data":[["1","2"],["3","4"]],"function":"function(){return '"foo bar'";}"}]
另一件事是:您不应该手动反序列化 JSON。所有较新版本的 jQuery 将根据响应的内容类型标头自动反序列化 JSON。
您已经将 ajax 请求的 dataType 设置为 json。响应是 JSON,将被反序列化。所以你可以直接使用
.success: function(response) {
console.log(response.data);
console.log(response.function);
}
从评论中回答问题/问题:
现在,您再次创建了无效的 JSON,因为"tickFormatter" : function(val, axis){return val.toExponential(2)},
忽略了函数字符串周围的引号。
- 在使用 str_replace() 插入字符串之前,您需要注意转义和引号。您可能需要一个小的帮助程序函数,它可以正确转义字符串 - 成为有效的 JSON。
- 您需要正确使用 str_replace():不替换外部引号,只替换内部引号
$tickFormatter$
。
等等.. 我将提供一个例子:
// we already have an array, which is JSON encoded
// now you want to insert additional JSON content.
// therefore we use a token replace approach.
// valid JSON - with $token$
$json = '[{"data":[["1","2"],["3","4"]],"tickFormatter":"$tickFormatter$"}]';
// the (future) javascript function is a string.
// it's not properly escaped or quoted to be JSON
// this allows for copy and pasting JS into PHP
$unescaped_functionString = 'function(){return "foo bar";}';
// in order escapre properly, we need a json escaping helper
/**
* @param $value
* @return mixed
*/
function escapeJsonString($value) { # list from www.json.org: ('b backspace, 'f formfeed)
$escapers = array("''", "/", "'"", "'n", "'r", "'t", "'x08", "'x0c");
$replacements = array("''''", "''/", "'''"", "''n", "''r", "''t", "''f", "''b");
$result = str_replace($escapers, $replacements, $value);
return $result;
}
// then we escape the function string
$functionString = escapeJsonString($unescaped_functionString);
// now its ready to be inserted into the existing JSON,
// where token $tickFormatter$ is defined
// the token must be in single quotes. you replace just the $token$ and not "$token$".
$json = str_replace('$tickFormatter$', $functionString, $json);
echo $json;
// The result is valid JSON again:
// [{"data":[["1","2"],["3","4"]],"tickFormatter":"function(){return '"foo bar'";}"}]
下一步:发出 ajax 请求,将 json 数据传输到客户端,并且您希望执行传入的函数。
所以新的问题是"如何在不使用 eval 的情况下从字符串调用/执行 JavaScript 函数?
通常,隧道是一种不好的做法,请参阅:在 JSON 结果中定义函数是否有效?
无论如何,有不同的方法可以做到这一点:
参考资料: http://everythingfrontend.com/posts/studying-javascript-eval.html
- eval() - 邪恶,但有效。
设置超时()
setTimeout(" function ", 0);
新功能()
var codeToExecute = "My.Namespace.functionName()"; var tmpFunc = new Function(codeToExecute); tmpFunc();
文档.写
document.write('<script> JS </script>')
数据 URI
var s = document.createElement('script'); s.src = 'data:text/javascript,' + encodeURIComponent('alert("lorem ipsum")') document.body.appendChild(s);
JSON 不能包含这样的函数。但是,您可以使用Javascript将函数注入DOM中,因此您要做的是跳过str_replace
部分,只需在DOM中创建一个<script type="text/javascript"></script>
元素并将data.function
属性插入其中。
- 我可以在json对象中添加一个函数吗
- 使用来自不同异步函数的响应创建一个json对象
- 使用JSON数据检索AJAX外部成功函数
- 将JSON字符串转换为函数参数
- 正在创建使用子字符串解析JSON数据的函数
- 解析Json内部的函数
- Ajax注释将json调用到函数中
- Javascript使用本地存储,回调对象并用Json更改为一个字符串以用于填充函数
- 使通过函数加载的JSON可用于其他函数
- 为什么从JSON文本到类型的转换不包括函数
- 如何使用类似JSON的JQuery函数
- 在“加载更多”按钮函数中包含 JSON 数据
- Json 返回每个函数
- 如何在 JSF 中将 JS 函数作为 JSON 属性传递给前端
- jQuery $.get() JSON 函数正在执行,但不会产生任何结果
- ajax get-json函数没有'不起作用
- 带有Ajax请求的Javascript JSON.parse函数n〃;新行转义字符
- '对象不是函数'当使用angular连接两个json数组时
- JSON.Stringify replacer函数无法获取键值
- JavaScript解析函数JSON.Parse没有按预期工作