且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在php中获得对单个ajax请求的多个响应

更新时间:2023-08-31 18:35:22

使用 JSON 作为数据类型在 PHP (后端)和 Javascript之间进行通信强>(前端).示例:

Use JSON as the datatype to communicate between PHP(Backend) and Javascript(Frontend). Example:

<? 
$person = array("name"=>"Jon Skeet","Reputation"=>"Infinitely Increasing");
header("Content-Type: application/json");
echo json_encode($person);
?>

Javascript/jQuery

$.ajax({
  url: "your_script.php",
  dataType: "JSON"

}).success(function(person) {
  alert(person.name) //alerts Jon Skeet
});