The function below makes an AJAX post. makepostrequest is just a standard ajax post request function I wrote that I have omitted since it's not the source of the problem. The function below does not send 'widgets'.
function widgets_positions(){
var widgets = '';
var col_1 = document.getElementById('col_1');
var col_2 = document.getElementById('col_2');
var col_3 = document.getElementById('col_3');
for(i = 0; i < col_1.childNodes.length; i++) {
var str1 = col_1.childNodes[i].className;
if(str1 && str1.match('widget')) widgets+='&c[1]['+i+']='+col_1.childNodes[i].id;
}
makePOSTRequest('/ajax.php',"widgets="+widgets);
return true;
}
BUT if in place of 'widgets' I try to post
var random = 'sumo'
makePOSTRequest('/ajax.php',"widgets="+random);
it works.
Not only that, if I开发者_开发问答 place an echo command in the above before 'makepostrequest'
, 'widgets'
get printed out on the clinetside as c[1]c[1]blahblah
.
So why does var random = 'sumo'
get sent but the 'widgets'
variable does not?
It's because 'widgets' starts with an &, which will mark the beginning of another variable definition in that request. So 'widgets' goes empty.
Try to remove that '&', and parse it accordingly in server-side to correctly read each widget.
May be it is cached? Clear browser cache and then if it works, add cache expiry in the ajax.php.
widgets+='&c[1]['+i+']='+col_1.childNodes[i].id;
makePOSTRequest('/ajax.php',"widgets="+widgets);
So you're going to end up with the form-encoded string:
widgets=&c[1][0]=foo&c[1][1]=bar
That is, a widgets
parameter with an empty string value, then a c[1][0]
parameter with foo
value, and so on. Is that what you wanted?
If you actually wanted to pack your whole &c[1][0]=foo&c[1][1]=bar
string into the widgets
parameter, the string you'd need would be:
widgets=%26c[1][0]=foo%26c[1][1]=bar
turning those ampersand separators into URL-encoded versions so they don't split the form data. To do that, you'd say:
makePOSTRequest('/ajax.php',"widgets="+encodeURIComponent(widgets));
精彩评论