繁体   English   中英

Jquery AJAX PHP 数据返回

[英]Jquery AJAX PHP data return

I'm using the ajax function in Jquery to return some values from a PHP script with the json_encode function. 返回的数据似乎充满了斜杠、引号和\r\n。 我知道 stripslashes 或 magic_quotes (已打开)一定有问题,但似乎无法获得干净的 output

确保在来自 jQuery 的 ajax 呼叫中,您告诉它期待 json 响应。 听起来您正在返回纯文本并尝试手动解析它。

$.ajax({
  url: "myscript.php",
  dataType: "json",
  success: function(data){
    console.log( data ); //this line only works with chrome (stock) or FireFox (with FireBug plugin)
  }
});

该代码将在您的控制台中回显(如果您没有带有 FireBug 的 chrome 或 FF,go 获得其中之一:P)json 编码的 output。 请记住,当您从 PHP 获得 output 时,您应该做的就是:

header('Content-type: application/json');
echo json_encode( $myAssociativeArrayOfData );
exit; //make sure nothing else happens to output something

您不需要使用任何特殊格式或斜线。 Just make sure the json code gets output as json code with the proper headers and jQuery's ajax function should convert it for you. 结果将是成功 function 中的数据变量是您的 json object(php 数组)。 因此,如果您传入这样的数组:PHP 中的 array('foo'=>'bar'),那么在 ZD223E1439188E478349D52476506C22E 中的成功 function 可以输入: 并得到一个对话框,上面写着“bar”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM