angularjs - 重构XMLHttpRequest到$ http

问题描述 投票:1回答:1

我想要以下代码

var request = new XMLHttpRequest();
    request.open("PUT", dbServer + "/configItemAdd");
    request.send(new FormData(form));
    request.onreadystatechange = function () {
        if(request.readyState == 4 && request.status == 200) {   window.location.reload(); }
};

看起来像这样

     const fd = new FormData(form);
     $http({
         method: 'PUT',
         url: dbServer + "/configItemAdd",
         data: fd
     }).then(function (resp){
         window.location.reload();
     });

问题是我收到两条错误消息:只有标签而没有文件输入我得到了SyntaxError:位于0的JSON中的意外的令牌#

并且使用文件输入我得到PayloadTooLargeError:请求实体太大

XMLHttpRequest代码正在使用和不使用输入文件。请帮忙:S

javascript angularjs http xmlhttprequest
1个回答
1
投票

您必须在您的请求中添加额外的标题,即{'Content-Type': 'multipart/form-data'}

const fd = new FormData(form);
 $http({
     method: 'PUT',
     url: dbServer + "/configItemAdd",
     Content-Type: 'multipart/form-data'
     data: fd
 }).then(function (resp){
     window.location.reload();
 });
© www.soinside.com 2019 - 2024. All rights reserved.