将 PHP json_encode 数组发送到 jQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4240763/
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
Sending PHP json_encode array to jQuery
提问by SIndhu
ok, i guess I need help ! I searched with every keyword I could think off, but I still cant figure out, please help. Am more of a php guy, and I've just started with jQuery.
好的,我想我需要帮助!我用我能想到的每个关键字搜索,但我仍然无法弄清楚,请帮助。我更像是一个 php 人,我刚开始使用 jQuery。
Basically, what I am trying to do is to send a jQuery post from a click function. And based on whatever is returned by my php function, show/hide 2 divs. My php function returns a "json_encode" array with 2 simple values, like such :
基本上,我想要做的是从点击函数发送一个 jQuery 帖子。并基于我的 php 函数返回的任何内容,显示/隐藏 2 个 div。我的 php 函数返回一个带有 2 个简单值的“json_encode”数组,例如:
//==================PHP code ==================================
//==================PHP 代码 ============================ ======
$message_for_user = "blah blah";
$calculatedValue = 1230;
$responseVar = array(
'message'=>$message_for_user,
'calculatedValue'=>$calculatedValue
);
echo (json_encode($responseVar));
//==================PHP code End ==================================
//==================PHP 代码结束 ============================ ========
My javascript code is supposed to accept the values returned by php :
我的 javascript 代码应该接受 php 返回的值:
//==================Javascript code ==================================
//==================Javascript 代码 ============================ ======
$("div.calculator_result").click(function()
{
$.post('myCalculator.php' ,{qid:itemID},function(response)
{
$("div.calculation_value").show(500).html(response['calculatedValue']);
$("div#message_for_user").show(500).html(response['message']);
}
}
//==================Javascript code End ==================================
//================Javascript 代码结束 ============================ ========
Unfortunately, on the javascript side of my project, the divs are not updated with the values returned by my php functions .... where am I wrong? I hope I was clear in my question, if not, do let me know, and I shall provide any extra info required.
不幸的是,在我的项目的 javascript 方面,div 没有使用我的 php 函数返回的值进行更新......我错在哪里?我希望我的问题很清楚,如果没有,请告诉我,我将提供所需的任何额外信息。
Another thing is that earlier, I was echo'ing only a single value, that is the calculated value (echo $calculatedValue), and everything worked fine, its only after I shifted to echo'in the json encode array that things dont work
另一件事是,早些时候,我只回显了一个值,即计算值(回显 $calculatedValue),一切正常,只有在我转移到 json 编码数组中的 echo' 之后,事情才行不通
回答by Kemo
var json = $.parseJSON(response); alert(json.message);
回答by lonesomeday
Try setting the dataType
option:
尝试设置dataType
选项:
$.post('myCalculator.php' ,{qid:itemID},function(response)
{
$("div.calculation_value").show(500).html(response['calculatedValue']);
$("div#message_for_user").show(500).html(response['message']);
}, 'json');
NB I have also added the closing brackets )
where you have missed them.
注意,我还在)
您遗漏的地方添加了右括号。
回答by wanovak
You must parse the JSON response. jQuery has this built-in functionality (thankfully, because otherwise IE6 and 7 don't natively support JSON). Set a variable equal to this:
您必须解析 JSON 响应。jQuery 有这个内置功能(谢天谢地,否则 IE6 和 7 本身不支持 JSON)。设置一个等于这个的变量:
$.parseJSON(response)
And then, if you're not familiar with JSON format, check the response headers (using Firebug or similar,) and that will help you pick which keys' values you want. If you're looping, I would look into for in statementsonce the response has been parsed.
然后,如果您不熟悉 JSON 格式,请检查响应头(使用 Firebug 或类似方法),这将帮助您选择所需的键值。如果您正在循环,我会在解析响应后查看for in 语句。
EDIT: Using $.getJSON, the parsing is done automatically. Write less, do more. :)
编辑:使用$.getJSON,解析是自动完成的。少写,多做。:)
回答by Wolfgang Leon
All you gotta do, its tell the Ajax call that you're receiving data type "json". In other words...
您要做的就是告诉 Ajax 调用您正在接收数据类型“json”。换句话说...
$.ajax({
url: "external_file",
method:"post",
dataType: "json", // **************** Note dataType****************
success:function(response){
console.log(response)
// Response will be a javascript array, instead of a string.
},
error: function(){
alert('something went wrong.')
}
})