开发者

Javascript Ajax help..multiple values [i]

开发者 https://www.devze.com 2023-04-12 13:03 出处:网络
What I\'m trying to do is post an array of messages asynchronously using this code. I spent over an hour trying to make it only post the four items in the array, but it keeps开发者_运维百科 posting gi

What I'm trying to do is post an array of messages asynchronously using this code. I spent over an hour trying to make it only post the four items in the array, but it keeps开发者_运维百科 posting gibberish in addition to the 4 items. Also, it doesn't redirect when done.

var a = document.body.innerHTML;
formx = a.match(/name="post_form_id" value="([\d\w]+)"/)[1]; 
dts = a.match(/name="fb_dtsg" value="([^"]+)"/)[1];    
composerid = a.match(/name="xhpc_composerid" value="([^"]+)"/)[1];
var msg = ['my first update', 
    'posting again', 
    'and again', 
    'done'
];

target = a.match(/name="targetid" value="([^"]+)"/)[1];
for (var i in msg) {
    pst = "post_form_id=" + formx +
          "&fb_dtsg=" + dts +
          "&xhpc_composerid=" + composerid +
          "&xhpc_targetid=" + target +
          "&xhpc_context=home&xhpc_fbx=1&xhpc_message_text=" + encodeURIComponent(msg[i]) +
          "&xhpc_message=" + encodeURIComponent(msg[i]) +
          "&UIPrivacyWidget[0]=40&privacy_data[value]=40&privacy_data[friends]=0&privacy_data[list_anon]=0&privacy_data[list_x_anon]=0&=Share&nctr[_mod]=pagelet_composer&lsd&post_form_id_source=AsyncRequest";

    with(newx = new XMLHttpRequest())
        open("POST", "/ajax/updatestatus.php?__a=1"),
        setRequestHeader("Content-Type", "application/x-www-form-urlencoded"),
        send(pst);                
}

redirect('http://apple.com');


I haven't looked at the code in depth because the formating is all messed up, but I bet the problem is on that for-in loop. for-in in Javascript is not a for-each loop and shouldn't be used to iterate over arrays. Use a normal for loop instead

for(var i=0; i<msgs.length; i++){

BTW, your code is full of bad practices, the worse of which is the use of the evil with statement.

0

精彩评论

暂无评论...
验证码 换一张
取 消