sending a tricky array in a httpclient

旧街凉风 提交于 2019-12-13 06:15:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!