XMLHttpRequest with Observable in Typescript

前端 未结 2 621
小蘑菇
小蘑菇 2021-02-19 08:54

I have a tslint problem when I try to manage the result of an XMLHttpRequest call I do to upload files. Here is my current method I found on the internet :

// Fi         


        
2条回答
  •  长发绾君心
    2021-02-19 09:19

    The simple way to achieve:

    xhrCall(url, formData, header) {
        return Observable.create(function (observer) {
          let xhr = new XMLHttpRequest();
          xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
              if (xhr.status == 200) {
                observer.next(xhr);
              } else {
                observer.error(xhr);
              }
            }
          };
    
          xhr.open("POST", url, true);
          xhr.setRequestHeader(header.name, header.value);
          xhr.send(formData);
        });
    }
    

提交回复
热议问题