Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php json响应中的函数名-不工作_Php_Jquery_Json - Fatal编程技术网

Php json响应中的函数名-不工作

Php json响应中的函数名-不工作,php,jquery,json,Php,Jquery,Json,我试图在JSON响应中发送一个函数,并试图在js中提醒该函数。下面是代码 JS: $('input').keyup(function(){ $.ajax({ type: "POST", url: "sample.php", dataType : 'json', data: 'val='+$('input').val(), success: function(response){ var j

我试图在JSON响应中发送一个函数,并试图在js中提醒该函数。下面是代码

JS:

$('input').keyup(function(){
    $.ajax({
        type: "POST",
        url: "sample.php",
        dataType : 'json',
        data: 'val='+$('input').val(),
        success: function(response){

         var json = transport.responseText.evalJSON();
         alert(json.function()); // => **should alert 'foo bar' - but not**
        }
    });
});
<?php
// Our sample array
$foo = array(
  'string'  => 'bar',
  'function'=> 'function(){return "foo bar";}'
);
$value_arr = array();
$replace_keys = array();
foreach($foo as $key => &$value){
  // Look for values starting with 'function('
  if(strpos($value, 'function(')===0){
    // Store function string.
    $value_arr[] = $value;
    // Replace function string in $foo with a 'unique' special key.
    $value = '%' . $key . '%';
    // Later on, we'll look for the value, and replace it.
    $replace_keys[] = '"' . $value . '"';
  }
}

// Now encode the array to json format
$json = json_encode($foo);

/* $json looks like:
{
  "number":1,
  "float":1.5,
  "array":[1,2],
  "string":"bar",
  "function":"%function%"
}
*/
// Replace the special keys with the original string.
$json = str_replace($replace_keys, $value_arr, $json);

// Send to to the client
echo $json;

/* This echoes the following string:
{
  "string":"bar",
  "function":function(){return "foo bar";}
}
*/
?>
PHP:

$('input').keyup(function(){
    $.ajax({
        type: "POST",
        url: "sample.php",
        dataType : 'json',
        data: 'val='+$('input').val(),
        success: function(response){

         var json = transport.responseText.evalJSON();
         alert(json.function()); // => **should alert 'foo bar' - but not**
        }
    });
});
<?php
// Our sample array
$foo = array(
  'string'  => 'bar',
  'function'=> 'function(){return "foo bar";}'
);
$value_arr = array();
$replace_keys = array();
foreach($foo as $key => &$value){
  // Look for values starting with 'function('
  if(strpos($value, 'function(')===0){
    // Store function string.
    $value_arr[] = $value;
    // Replace function string in $foo with a 'unique' special key.
    $value = '%' . $key . '%';
    // Later on, we'll look for the value, and replace it.
    $replace_keys[] = '"' . $value . '"';
  }
}

// Now encode the array to json format
$json = json_encode($foo);

/* $json looks like:
{
  "number":1,
  "float":1.5,
  "array":[1,2],
  "string":"bar",
  "function":"%function%"
}
*/
// Replace the special keys with the original string.
$json = str_replace($replace_keys, $value_arr, $json);

// Send to to the client
echo $json;

/* This echoes the following string:
{
  "string":"bar",
  "function":function(){return "foo bar";}
}
*/
?>

函数在JSON中不是有效的数据类型。您可以做的是将函数表达式作为字符串发送:

{
  "string": "bar",
  "function":"function(){return \"foo bar\";}"
}
然后使用
eval

var f = eval(response['function']);
但我不建议这样做。这很可怕,很复杂,不容易理解或维护。这基本上是你能做的最糟糕的事情

你想用它解决什么问题


如果必须从服务器发送JavaScript,那么就这样做(不要返回JSON),并使用
script
,因为
dataType

没有任何函数表示。这是一种数据符号。即使您欺骗PHP发送函数(就像您试图做的那样),客户端上正确的JSON反序列化程序也会抛出异常(理想情况下)。一些非常幼稚的函数依赖于
eval
可以传递函数,但此时,您使用的不是JSON,而是JavaScript

如果你想发回代码,你需要发回JavaScript

另外,在这一行:

alert(json.function());
…您有语法错误<代码>函数是JavaScript中的保留字。不能将其用作属性名称文字。如果您创建了一个名为“function”的属性(您可以,但可能不应该),要访问它,您必须使用字符串符号来访问属性:

alert(json["function"]());

另请参见,您似乎将一些原型代码(
evalJSON
)混合到了一些东西中

但是他们正在使用而不是

transport.responseText.evalJSON()在jQuery中不表示任何内容


再看看,你会发现函数不应该放在JSON中。。有关更多解释,请阅读

eval
和json中编码为代码字符串的函数。这太可怕了:(+1)值可以是双引号中的字符串、数字、true或false或null、对象或数组。这些结构可以嵌套。请始终记住,json只是结构化的文本。