javascript PHP:如何提取ajax数组响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4231708/
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
PHP: how to extract ajax array response
提问by gautamlakum
I am getting ajax response in array format from php url. How to extract array response values in jQuery? FYI:
我从 php url 得到数组格式的 ajax 响应。如何在 jQuery 中提取数组响应值?供参考:
PHP array is:
PHP数组是:
$response = array('msg' => 'Hello', 'html' => '<b>Good bye</b>');
I am getting $response array in my ajax response. i.e.
我在我的 ajax 响应中收到了 $response 数组。IE
var promo = "promo=45fdf4684sfd";
$.ajax({
type: "POST",
url: baseJsUrl + "/users/calc_discount",
data: promo,
success: function (msg) { // I am getting $response here as ajax response.
//alert(msg);
// Here I want to check whether response is in array format or not. if it is in array format, I want to extract msg here and want to use response array values.
}
});
Let me know answer pls. Thanks.
请让我知道答案。谢谢。
回答by alex
You should echo that $responsewith json_encode().
你应该$response用json_encode().
You should probably set dataType: 'json'too inside the object literal you send to $.ajax().
您可能dataType: 'json'也应该在发送到的对象文字中进行设置$.ajax()。
Then you can access it natively with JavaScript using the dot operator inside your success callback...
然后,您可以使用成功回调中的点运算符通过 JavaScript 本地访问它...
function(msg) {
alert(msg.html);
}
BTW, this line...
顺便说一句,这条线...
$response = array(['msg'] => 'Hello', 'html' => '<b>Good bye</b>');
... isn't valid PHP. Remove the brackets from the first key.
...不是有效的 PHP。从第一把钥匙上取下括号。
回答by Māris Kise?ovs
My favorite solution for this is to encode array with PHP's function json_encode() so jquery will be happy to parse it.
我最喜欢的解决方案是使用 PHP 的函数 json_encode() 对数组进行编码,这样 jquery 就会很乐意解析它。
回答by Emil Vikstr?m
I presume that you mean a JSON response, like this:
我认为您的意思是 JSON 响应,如下所示:
{"msg":"Hello","html":"<b>Good bye<\/b>"}
This is actually a native JS object, so you can use it right away like this:
这实际上是一个原生 JS 对象,因此您可以像这样立即使用它:
success: function(msg){
alert(msg.msg);
alert(msg.html);
}
You can also use the jQuery.each()function to loop over all properties of the JSON object if you need to:
如果需要,您还可以使用jQuery.each()函数循环遍历 JSON 对象的所有属性:
jQuery.each(msg, function(key, val) {
alert(key + "=" + val);
});
回答by Satya Prakash
and If you do not have control over the PHP output then you can use another method to get the result. Another solution is using http://phpjs.org/library. Here you can find many functions available in JS as available in php. Usage is also same as that of PHP. So I feel if you get the json_encode/json_decode from there and use that then it can solve your problem easily.
如果您无法控制 PHP 输出,那么您可以使用另一种方法来获取结果。另一种解决方案是使用http://phpjs.org/库。在这里你可以找到许多在 JS 中可用的函数,在 php 中可用。用法也与 PHP 相同。所以我觉得如果你从那里得到 json_encode/json_decode 并使用它,那么它可以轻松解决你的问题。
Remember you can compile your needed functions only. In your case, it is json_encode and json_decode. No need to download whole library. Url to compile your library: http://phpjs.org/packages/configure
请记住,您只能编译所需的函数。在您的情况下,它是 json_encode 和 json_decode。无需下载整个库。用于编译库的 URL:http: //phpjs.org/packages/configure

