如何解决https POST请求?

时间:2019-10-29 09:48:14

标签: javascript class https httprequest

我有一个HTTP客户端类,它有一个post()方法。如果我尝试使用这种方法将数据发送到服务器,我将一无所获。

我试图与Postman发送数据,效果很好。所以我认为这是有问题的,但是我不知道出了什么问题。

这是HttpClient的代码:

export class HttpClient {
 constructor() {
    this.xhr = new XMLHttpRequest();
     this.xhr.onreadystatechange = () => {
         switch (this.xhr.readyState) {
            case 0:

                 break;
             case 1:

                break;
             case 2:

                 break;
             case 3:

                 break;
             default:

         }
    };
     this.xhr.onload = () => {
         return this.xhr.response;
     };     }

     post(url, async, data) {
         return new Promise((resolve,reject) => {
                 this.xhr.open("POST", url, async);
                 this.xhr.setRequestHeader("Content-Type", "applicatoin/json");
                this.xhr.send(data);
                resolve(this.xhr.response);
             },
                reason => {
              //DO SOMETHING
             }
         );
     }

这是我尝试使用此HttpClient的代码:

addButton() {
   if (this.items.length < 9) {
         const task = {
           "name" : document.getElementById("Myinput").value,
           "status" : false
       };
        this.httpClient
        .post("http://todoapp.test/api/create", true, task)
          .then(resolve => {
       //DO SOMETHING
          })
         .catch(error => console.log(error));
     }
}

为什么在POST请求的than方法中解析没有价值?

0 个答案:

没有答案