如何使用jquery和ajax更新json中的数据

时间:2015-09-08 08:23:38

标签: javascript jquery ajax json

我正在尝试更新我的json如果uid = 0090000219我想要更改我的"secret"

我有这个json:

[
{
"id":23,
"uid":"0090000219",
"cid":"0090000013",
"extension":"201",
"secret":"Myojyo42_f",
"leader":true,
"simultaneous":false,
"confbridge_id":17,
"created_at":"2015-09-02 12:49:12",
"updated_at":"2015-09-02 12:49:12"
},
{
"id":24,
"uid":"0090000221",
"cid":"0090000013",
"extension":"203",
"secret":"Myojyo42_f",
"leader":false,
"simultaneous":false,
"confbridge_id":17,
"created_at":"2015-09-02 12:49:12",
"updated_at":"2015-09-02 12:49:12"
},
{
"id":25,
"uid":"0090000220",
"cid":"0090000013",
"extension":"202",
"secret":"Myojyo42_f",
"leader":false,
"simultaneous":false,
"confbridge_id":17,
"created_at":"2015-09-08 07:40:04",
"updated_at":"2015-09-08 07:40:04"
},
{
"id":26,
"uid":"0090000223",
"cid":"0090000013",
"extension":"205",
"secret":"Myojyo42_f",
"leader":false,
"simultaneous":false,
"confbridge_id":17,
"created_at":"2015-09-08 07:40:04",
"updated_at":"2015-09-08 07:40:04"
}
]

这是我的代码,但它没有更新:

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
var conf_url = "https://192.168.236.33/confbridge_participants/conference_participants.json?cid=0090000013";
  getParticipant(conf_url, function(data){

for (var i=0; i<data.length; i++) {
  if (data[i].uid == 0090000219) {
    data[i].secret = "Secret";
    break;
  }
}

alert("New secret: " + data[2].secret);
  });
     function getParticipant(conf_uri, handleData) {
        $.ajax({
          type: "GET",
          url: conf_uri,
          dataType: "jsonp",
          jsonpCallback: 'callback',
          contentType: "application/javascript",
          success: function(data) {
            handleData(data);
         //   console.log(data);
          }
        });

      }
</script>

1 个答案:

答案 0 :(得分:0)

您正在查看错误的值以检查其是否成功。你需要检查数组中的第一个对象,而不是第3个...

alert("New secret: " + data[0].secret);