weixin_33698823 2013-02-07 01:11 采纳率: 0%
浏览 33

使用返回的json代码

I'me trying to use ajax with php, I have the follogin script in php :

<?php

// this file get the POST infor sent by a AJAX request and will return the value is succesful.

$price['name'] = "Called";
$price['Wheel']  = 75.25;
$price['Tire']   = 50.00;

echo json_encode($price);
?>

and I'm calling this code from my main page in the following way :

        $.post("ajax/profileMod.php", {
            'lname':lname,
            'fname':fname,
            'mname':mname,
            'language':language,
            'title':title,
            'ptype':ptype,
            'vip':vip,
            'vreason':vreason
        })
        // Retreive the data from the php script
        .done(function(data) {
         // php code : echo json_encode(array("name"=>"Called!"));
            alert(data);
        }, "json");

        // Stop original behavior
        return false;
    });

The returniong result from the alert is the following test : {"name":"Called",Wheel":75.25,"Tire":50}

How can I change this result so I may use it in the following way in javascript EX:

alert(myresult['Name']) ; Would give me "Called".

So I basicly would like a associative array in javascript, but I read somewhere on this forum that you can't have associative array in Javascript, only object...

Please help!

  • 写回答

1条回答 默认 最新

  • weixin_33725722 2013-02-07 01:14
    关注

    Pass "json" as the last parameter to .post() to tell jQuery to parse the response as JSON.
    (or, fix your server to return the correct Content-Type of application/json, and jQuery should do that automatically)

    You will then get a Javascript object, allowing you to write

    alert(result.name);
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!