Ajax的数组提交

对于HTML的Form来说,直接在命名后加[]就可以实现数组提交了,但对Ajax来说,这样好像不行。自己试了一下,发现在直接用多个相同变量可以用做数组提交。

Ajax的代码可以参考:Ajax的使用

如提交一个selectedId的数组{1,2,3},可以把Param设为"selectedId=1&selectedId=2&selectedId=3",在服务器就可以用selectedId数组取得"1","2","3"三个值,代码如下:

JavaScript
var Ajax = function(option) {
     var request;
     var CreateRquest = function() {
         var httpRequest;
         try {
             httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
         } catch(e) {
             try {
                 httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
             } catch(e1) {
                 httpRequest = new XMLHttpRequest();
             }
         }
         return httpRequest;
     };
     var SendRequest = function() {
         request = CreateRquest();
         request.open("post", option.Url, true);
         request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

         request.onreadystatechange = ResponseRequest;
         request.send(option.Param);
     };
     var ResponseRequest = function() {
         if (request.readyState == 4) {
             if (request.status == 200) {
                 option.Success(request);
             } else {
                 option.Failure(request);
             }
         }
     };
     SendRequest();
};
var OnClick = function (el) {
     var option = {
         Url : "test.php",
         Param : "selectedId=1&selectedId=2&selectedId=3",
         Success : function(request) {
             alert(request.responseText);
         },
         Failure : function(request) {
             alert(false);
         }
     };
     new Ajax(option);
};

转载请注明:宇托的狗窝 » Ajax的数组提交

发表我的评论
取消评论

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址