English 中文(简体)
PHP: 如何提取亚克斯阵列反应
原标题:PHP: how to extract ajax array response

I am getting ajax response in array format from php url. How to extract array response values in jQuery? FYI:

PHP阵列:

$response = array( msg  =>  Hello ,  html  =>  <b>Good bye</b> );

我正在从我的阿贾克斯对策中找到应急阵列。 i.e.

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.

最佳回答

请注意$response with json_encode(

可能的话,请在标本内贴上<条码>数据表:json 。

然后,你可以利用信使的操作者在你的成功呼声中,从本土上进入 Java。

function(msg) {
    alert(msg.html);
}

BTW, this line...

$response = array([ msg ] =>  Hello ,  html  =>  <b>Good bye</b> );

http://codepad.org/Efbcf002“rel=“noretinger”>isn t valid PHP。 删除第一关键部分的方括号。

问题回答

我赞成这样做的办法是,用PHP功能json_encode(),这样分类将乐于加以压缩。

我假定,你指的是一位智者的反应,例如:

{"msg":"Hello","html":"<b>Good bye</b>"}

这实际上是一个本土的“共同财产”目标,因此,你可以使用它。

success: function(msg){
   alert(msg.msg);
   alert(msg.html);
}

您也可使用jQuery.each()功能,在满足以下要求的前提下,处理JSON物体的所有特性:

jQuery.each(msg, function(key, val) {
  alert(key + "=" + val);
});

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.

各位只能汇编必要的职能。 在你的情况下,是json_encode和json_decode。 无需下载整个图书馆。 http://phpjs.org





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签