HTTP请求中的400错误请求,但在ionic中的邮递员中却能正常工作

时间:2019-06-23 07:24:15

标签: angular ionic-framework ionic3

当我启动http.post()时,它的响应为“ 400(错误请求)”,但是相同的代码在另一个项目中使用表单数据发送数据,但在此项目中,我使用application / json发送数据以行格式。

API与邮递员配合良好并生成令牌

这是我的邮递员代码,可以正常工作

    var http = require("https");

      var options = {
         "method": "POST",
         "hostname": "dhc-app-api.azurewebsites.net",
         "port": null,
         "path": "/v2/auth/signIn",
         "headers": {
         "content-type": "application/json",
        "cache-control": "no-cache",
       "postman-token": "e9ba66bd-e56b-e526-e6c0-d1b3449760f2"
         }
        };

        var req = http.request(options, function (res) {
         var chunks = [];

        res.on("data", function (chunk) {
        chunks.push(chunk);
      });

      res.on("end", function () {
      var body = Buffer.concat(chunks);
      console.log(body.toString());
       });
       });

      req.write(JSON.stringify({ email: 'manojmht45@gmail.com', 
       password: '********' }));
      req.end();

这是我的离子代码

         postData(){
              return new Promise((resolve, reject) =>{
            let headers = new Headers(
                {
                    'Content-Type' : 'application/json'
                });
            let options = new RequestOptions({ headers: headers });

            let data = JSON.stringify({
                "email":"manojmht45@gmail.com",
                "password":"********"
            });

            return new Promise((resolve, reject) => {
                this.http.post('https://dhc-app-api.azurewebsites.net/v2/auth/signIn', data, options)
                    .toPromise()
                    .then((response) =>
                    {
                        console.log('API Response : ', response.json());
                        resolve(response.json());
                    })
                    .catch((error) =>
                    {
                        console.error('API Error : ', error.status);
                        console.error('API Error : ', JSON.stringify(error));
                        reject(error.json());
                    });
            });
        });

邮递员结果

        {
"token": "eyJhbGciOiJIUzI1NiICI6IkpXVCJ9.eyJlbWFpbCI6ImN1aV9ib21iMDIwNEBvdXRsb29rLmNvbSIsImFjY291bnRJZCI6ImxYRVhPMFZqXNlcklkIjoiNXJ5NzNaeUQiLCJyb2xlIjoidXNlciIsImFjY2Vzc09wZXJhdGlvbiI6IltPcGVyYXRpb25dLltSZWdpb25dLltBbGxdIiwiZGVmYXVsdE9wZXJhdGlvbiI6IltPcGVyYXRpb25dLltSZWdpb25dLltBbGxdxpZW50IjoiRGVtbyIsIm5hbWUiOiJEYXZpZCBCdWNjaSIsIm1hZ2ljTGluayI6ZmFsc2UsImlhdCI6MTU2MTI3MTUwMywiZXhwIjoxNTYxMjczOTAzfQ.71asfbf2Nop-QeqVbi2YswnnkY-5qHrhTRjSUZlFKI"
      }

我的离子代码结果

    polyfills.js:3 OPTIONS https://dhc-app-api.azurewebsites.net/v2/auth/signIn 400 (Bad Request)



     (index):1 Access to XMLHttpRequest at 'https://dhc-app-api.azurewebsites.net/v2/auth/signIn' from origin 'http://localhost:8100' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
     auth-service.ts:47 API Error :  0

    auth-service.ts:48 API Error :  {"_body":{"isTrusted":true},"status":0,"ok":false,"statusText":"","headers":{},"type":3,"url":null}

0 个答案:

没有答案