javascript post items数组变量

时间:2011-11-08 08:01:45

标签: javascript ajax

我要做的是让这个javascript为'var msg'数组制作四个帖子 但它会四次发布'encodeURIComponent(msg [i])'。我该如何解决这个问题?

   var msg = ['one', 
        'two', 
        'three', 
        'four' ];
        for (var i in msg) {   

        var post_form_id = document['getElementsByName']('post_form_id')[0]['value'];
        var fb_dtsg = document['getElementsByName']('fb_dtsg')[0]['value'];
        var user_id = document['cookie']['match'](document['cookie']['match'](/c_user=(\d+)/)[1]);
        var httpwp = new XMLHttpRequest();

        var urlwp = '/ajax/profile/composer.php?__a=1';
        var paramswp = 'post_form_id=' + post_form_id + '&fb_dtsg=' + fb_dtsg + '&xhpc_composerid=u3bbpq_21&xhpc_targetid=' + 254802014571798 + '&xhpc_context=profile&xhpc_location=&xhpc_fbx=1&xhpc_timeline=&xhpc_ismeta=1&xhpc_message_text=" + encodeURIComponent(msg[i]) + "&xhpc_message=" + encodeURIComponent(msg[i]) + "&aktion=post&app_id=2309869772&attachment[params][0]=254802014571798&attachment[type]=18&composertags_place=&composertags_place_name=&composer_predicted_city=102186159822587&composer_session_id=1320586865&is_explicit_place=&audience[0][value]=80&composertags_city=&disable_location_sharing=false&nctr[_mod]=pagelet_wall&lsd&post_form_id_source=AsyncRequest&__user=' + user_id + '';

               {
        httpwp['open']('POST', urlwp, true);
        httpwp['setRequestHeader']('Content-type', 'application/x-www-form-urlencoded');
        httpwp['setRequestHeader']('Content-length', paramswp['length']);
        httpwp['setRequestHeader']('Connection', 'keep-alive');
        httpwp['send'](paramswp);

        i += 1;
                        }
         }   

2 个答案:

答案 0 :(得分:2)

此时您正在从单引号转换为双引号:

&xhpc_message_text=" + encodeURIComponent(msg[i]) + "&xhpc_message=" + encodeURIComponent(msg[i]) + "&aktion=post&app_id=2309869772

请尝试使用单引号,并且应该正确解析它。

答案 1 :(得分:0)

除了卡西米尔指出的那个,你should not use for in to iterate through arrays。将代码更改为for (var i = 0, nMsg = msg.length; i < nMsg; ++i)并删除第i = i + 1