"yes",-6ren">
gpt4 book ai didi

javascript - Ajax JSON 访问数组,从 JSON 编码服务器响应数组

转载 作者:行者123 更新时间:2023-11-28 00:30:20 25 4
gpt4 key购买 nike

当 ajax JSON 抛出 post 请求时,我有一个数组 JSON 编码响应(请参阅下文)。

requestparser.php:

$array = array("phweb" => "yes", "phemail" => "yeeess");
echo json_encode($array);

此 Ajax JSON 用于将 post 请求发送到 requestparser.php 并处理返回响应。

$.ajax({
type: 'POST',
url: 'requestparser.php',
data: { "request" : "pull" },
contentType: "application/json; charset=utf-8",
dataType: 'json',
cache: false,
success: function(result) {
alert(result[0]);
alert(result[1]);
}
});

我想获取数组键 phweb 的值和数组键 phemail 的值,但是当弹出警报框时,它显示 undefined >。看起来有什么问题吗?任何帮助、想法、线索将不胜感激。

到目前为止我尝试过的是:

$.ajax({
type: 'POST',
url: 'requestparser.php',
data: { "request" : "pull" },
contentType: "application/json; charset=utf-8",
dataType: 'json',
cache: false,
success: function(result) {
alert(result[0]->phweb);
alert(result[1]->phemail);
}
});

遗憾的是,它不起作用。

最佳答案

结果是一个 JSON 对象。您可以像这样访问它

success: function(result) {
alert(result['phweb']);
alert(result['phemail']);
}

关于javascript - Ajax JSON 访问数组,从 JSON 编码服务器响应数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29062116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com