将jQuery.ajax转换为XMLHttpRequest时出错

时间:2012-03-15 21:48:00

标签: javascript xmlhttprequest

我正在努力使用XMLHttpRequest对象转换以下$ .ajax:

function doPost(url, queryString, successCallback) {
   $.ajax({
      type : 'POST',
      url : url,
      dataType : 'json',
      data : queryString,
      success : successCallback,
      error : function(jqXHR, textStatus, errorThrown) {
         alert('ajax:error-' + errorThrown);
      },
      timeout : function() {
         alert('ajax:timeout');
      },
      abort : function() {
         showWaitCursor(false);
         alert('ajax:abort');
         appendLog('doPost:abort');
      },
      parsererror : function() {
         alert('ajax:parsererror');
      }
   });
}

以下内容有什么问题:

function doPost2(url, queryString, successCallback) {
   var kvs = queryString.split("&");

   var formData = new FormData();
   for(var i = 0; i < kvs.length; i++) {
      var kv= kvs[i].split("=");
      var key = kv[0];
      var value = kv[1];
      formData.append(key, value);
   }

   var request = new XMLHttpRequest();
   request.addEventListener('load', successCallback, false);

   request.open("POST", url, true);
   request.setRequestHeader("Accept","application/json");
   request.setRequestHeader("Content-Type", "application/json");
   request.send(formData);
}

回调event.target.responseText是数字值中的“意外字符(' - '(代码45)):跟随减号的预期数字(0-9),用于有效数值  在[来源:org.apache.catalina.connector.CoyoteInputStream@547b6d1c; line:1,column:3]“

上面的doPost2有什么问题?

0 个答案:

没有答案