使用Ajax将元素附加到Json响应

时间:2016-06-28 14:47:10

标签: javascript json

我在解决这个问题时遇到了一些麻烦。我有一个Json回复:

[
  {
    "id": 82797,
    "name": "pom-ta-all",
    "date": "2016-06-26T11:57:53+0000",
    "creationDate": "2016-06-10T14:15:00+0000",
    "version": "1.0.0",
  },
  {
    "id": 80926,
    "name": "pom-evnthdl-wln-all",
    "creationDate": "2016-06-03T17:44:20+0000",
    "version": "1.1.0",
  }...

我是从使用Jquery Ajax的rest api获得的。我如何在Ajax调用中将当前日期添加到每个元素,使其看起来如此:

[
      {
        "id": 82797,
        "name": "pom-ta-all",
        "date": "2016-06-26T11:57:53+0000",
        "creationDate": "2016-06-10T14:15:00+0000",
        "version": "1.0.0",
        "currentDate": "2016-06-28"
      },...

我可以使用.append()吗?如果是这样,怎么样?我一般不熟悉Ajax和JavaScript。如果格式如此,那将是最好的:

var job = $.ajax({
    url: url,
    dataType: 'json',
    data: data,
    success: #insert magic here, <---
    error: function()
  });

感谢您的想法

1 个答案:

答案 0 :(得分:1)

您的成功功能

.war

工作示例,基于您的inputData

&#13;
&#13;
...
success: function(json) {
    $.each(json, function(idx) {
       json[idx].currentDate = new Date();
    });
}
...
&#13;
var json = [
  {
    "id": 82797,
    "name": "pom-ta-all",
    "date": "2016-06-26T11:57:53+0000",
    "creationDate": "2016-06-10T14:15:00+0000",
    "version": "1.0.0",
  },
  {
    "id": 80926,
    "name": "pom-evnthdl-wln-all",
    "creationDate": "2016-06-03T17:44:20+0000",
    "version": "1.1.0",
  }];
  
  
  
  $.each(json, function(idx) {
     json[idx].currentDate = new Date();
  });
  
  console.log(json);
&#13;
&#13;
&#13;

如何获得使用JS格式化的当前日期,请参阅其他人的建议:

How do I get the current date in JavaScript?