php 从 JQuery 读取“echo json_encode()”的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6599215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Correct way to read 'echo json_encode( )' from JQuery
提问by SirBT
I am using: echo json_encode($Response); to send an Associative array back to JQuery Ajax. Whenever I try to read each ID key value I get an undefined value. Please help me figure out what I am doing so wrong... Thanks in advance
我正在使用:echo json_encode($Response); 将关联数组发送回 JQuery Ajax。每当我尝试读取每个 ID 键值时,我都会得到一个未定义的值。请帮我弄清楚我做错了什么......提前致谢
My PHP Code:
我的 PHP 代码:
$Stuff = 'Hello world';
$Success = true;
$Content = $Stuff;
$Response = array('Success' => $Success, 'Content' => $Content);
echo json_encode($Response);
##
My JS code:
我的JS代码:
var sFirstName = $('#student_first_name').attr('value');
$.ajax({
type: "GET",
url: "../pgs/UpdateEditAStudent.php",
data: "FirstName="+ sFirstName ,
//The below code will give me: {"Success":true,"Content":"Hello world"}
success: function(data){$("#Ajax_response").html(data);}
//The popup window will show me "Undefined"
//and: {"Success":true,"Content":"Hello world"}
success: function(data){$("#Ajax_response").html(data); alert(data.Content);}
});
回答by Lumbendil
You should set the mime type aswell, wich, according to this questionis application/json
. Then jQuery will understand the answer is a json element. To do it, you'd do the following:
您还应该设置 mime 类型,根据这个问题是application/json
. 然后jQuery就会明白答案是一个json元素。为此,您需要执行以下操作:
header('Content-Type: application/json');
In your UpdateEditAStudent.php
before printing anything.
在您UpdateEditAStudent.php
打印任何东西之前。
回答by Anthony Simmon
You don't have to add a header to the PHP file, just use this Jquery parseJSON function:
您不必向 PHP 文件添加标题,只需使用此Jquery parseJSON 函数:
Keep this PHP code as it is:
保持这个 PHP 代码不变:
$Stuff = 'Hello world';
$Success = true;
$Content = $Stuff;
$Response = array('Success' => $Success, 'Content' => $Content);
echo json_encode($Response);
And for the JS:
对于 JS:
$.ajax({
type: "GET",
url: "../pgs/UpdateEditAStudent.php",
data: "FirstName="+ $('#student_first_name').val(),
success: function(data){
// Here is the tip
var data = $.parseJSON(data);
alert(data.Content);
}
});
回答by Niklas
You need to define the correct dataType
or provide the correct header, as Lumbendil described.
您需要定义正确的dataType
或提供正确的标题,如 Lumbendil 所述。
You can manually define the dataType
to json
, so your code would look like:
您可以手动定义dataType
to json
,因此您的代码如下所示:
$.ajax({
type: "GET",
url: "../pgs/UpdateEditAStudent.php",
data: "FirstName="+ sFirstName ,
dataType: "json",
...etc
回答by Stefan H Singer
It's an array. You should probably do alert(data['Content']);.
这是一个数组。你可能应该做 alert(data['Content']);。
回答by asdasd
do something like this
做这样的事情
$Stuff = 'Hello world';
$Success = true;
$Content = $Stuff;
$Response = array('Success' => $Success, 'Content' => $Content);
echo json_encode($Response);