问题
I have to integrate data from an array into a webservice call which isn't the most efficient but it is what it is.
I have an array of ids (friend facebook ids). I need to send these id's as parameters in a http client in titanium. Due to Titanium having some trouble with passing arrays in webservices, I need to construct the send method of my http client as such:
non_xhr.send('user_id=100005941351187&friend_ids[0]=100000049956179&friend_ids[1]=100005272411678');
Obviousy depending on the user, they will have a different number of results to be stored in the array previously mentioned (of facebook friend ids).
I need help in how to integrate a loop based on the length of array mentioned above in order to construct the parameters needed, as described above.
All help appreciated.
I am using Titanium but for the purposes of this question, it is basically just javascript
回答1:
How about creating your params like that:
function createParams(userId, friendIds) {
var output = "user_id=" + userId;
for(var i = 0, max = friendIds.length; i < max; i++) {
output += "&friend_ids[" + i + "]=" + friendIds[i];
}
return output;
}
You can find a working fiddle here.
来源:https://stackoverflow.com/questions/19366024/sending-a-tricky-array-in-a-httpclient