English 中文(简体)
j 质量:如何加入2项 requests子的要求?
原标题:jQuery: how to join 2 results of requests to json server?

我想知道我们是否能够做这样的事情。

 var joinedJSON;
 $.get("server.json?action=type&type=image", function(json) {joinedJSON +=json ; }
 $.get("server.json?action=type&type=jpg", function(json) {joinedJSON +=json ; }
 $.get("server.json?action=type&type=png", function(json) {joinedJSON +=json ; }
 $.get("server.json?action=type&type=tiff", function(json) {joinedJSON +=json ; }

每一份请求都会为我们提供这样的初等人物数据:

[
    {
        "href": "bf051e8675b11c72eec781e855593589a086d2295378b96a8b7269c31b8fa673.user.file",
        "title": "Привет Мир.jpg",
        "user_name": "[email protected]",
        "modified": "2012-01-16 07:24:11",
        "is_public": 0,
        "size": 65516
    },
    {
        "href": "abd01be9a0830579d6366e48fc0c48d4c7cc350d80719843ca84c782346626f6.user.file",
        "title": "",
        "user_name": "[email protected]",
        "modified": "2012-01-16 07:24:19",
        "is_public": 0,
        "size": 89782
    },
    {
        "href": "0a27fd3b563b2877c3a072648e0f7c2a57539f3aba4ce688c7774eca6b70774e.user.file",
        "title": "Привет Мир 2.jpg",
        "user_name": "[email protected]",
        "modified": "2012-01-16 07:24:29",
        "is_public": 1,
        "size": 58574
    }
]

考虑到某些项目可能重复(C++的教材),我们能否获得一个很长的阵列?

最佳回答

请注意,在Javascript中未对“Arrays”的添加操作+。 它不把一个阵列延伸到另一个阵列,而是强迫它们进行扼杀。

[1,2,3] + [4,5,6] // will produce  1,2,34,5,6 

页: 1 而是

[1,2,3].concat([4,5,6])

此外,请注意<代码>$.get是一个同步的功能。 如果你简单地将其编成你提供的法典,你将不知道何时完成所有要求。 相反,你应努力在以前的呼吁职能中发出每一项请求。 这确保在收到以前的答复后提出每一项请求。

var joinedJSON = [];
$.get("server.json?action=type&type=image", function(json) {
  joinedJSON.concat(json);
  $.get("server.json?action=type&type=jpg", function(json) {
    joinedJSON.cancat(json);
  }
}

如果你发现nes背,你可以考虑使用。 The async model

问题回答

一旦您(或您的代码)将返回的JSON阵列划成阵列,你就可使用Cconcat()把这两个阵列混成一个阵列。

var jsontotal = json1.concat(json2);




相关问题
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.

热门标签