IONIC发布请求投掷错误

时间:2018-01-26 14:20:49

标签: angular laravel ionic-framework ionic3

我正在尝试从我的离子3应用程序到我的laravel api进行POST登录api调用。 但是,当我试图获取数据时,我会发现错误,我不太确定原因是什么。

我的代码 在提供者

postData(credentials, type){
    return new Promise((resolve,reject)=>{
      let headers = new Headers();
      headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

      this.http.post(
          apiUrl+type,
          JSON.stringify(credentials),
          { headers: headers }
      ).subscribe(res=>{
        resolve(res.JSON());
      }),(err)=>{
        reject(err);
      }
    });
  }

从我的登录页面

responseData:any;
  userData={"provider":"","email":"","password":""};
login() {
    //Get email address and password and validate
    this.authService.postData(this.userData,"login").then((result)=>{
      this.responseData = result;
      localStorage.setItem('userToken',JSON.stringify(this.responseData));
      //console.log(this.responseData);
      console.log(this.userData);
    },(err)=>{
      //connection failed message
    });

    // Once validated go to home page
    //this.nav.setRoot(HomePage);

  }

所以,当我打电话给api时,我希望它能给我

{"status":200,"msg":"Successfully loged in","token":"Z82VWTdABK9hcEvnng5kGR2NWW9iX1giewX5B6fw8jUFhVpN1OjIZPKGcwq1"}

但不是这样,我得到了这个

POST http://localhost:8100/://localhost:8000/api/login 404 (Not Found)

ERROR Response {_body: "<!DOCTYPE html>↵<html lang="en">↵<head>↵<meta char…//localhost:8000/api/login</pre>↵</body>↵</html>↵", status: 404, ok: false, statusText: "Not Found", headers: Headers, …}headers: Headers_headers: Map(7) {"content-security-policy" => Array(1), "x-content-type-options" => Array(1), "connection" => Array(1), "x-powered-by" => Array(1), "content-length" => Array(1), …}_normalizedNames: Map(7) {"content-security-policy" => "content-security-policy", "x-content-type-options" => "x-content-type-options", "connection" => "connection", "x-powered-by" => "x-powered-by", "content-length" => "content-length", …}__proto__: Objectok: falsestatus: 404statusText: "Not Found"type: 2url: "http://localhost:8100/://localhost:8000/api/login"_body: "<!DOCTYPE html>↵<html lang="en">↵<head>↵<meta charset="utf-8">↵<title>Error</title>↵</head>↵<body>↵<pre>Cannot POST /://localhost:8000/api/login</pre>↵</body>↵</html>↵"__proto__: Bodyconstructor: ƒ Response(responseOptions)toString: ƒ ()__proto__: Object

我确实检查了postman localhost:8000/api/login它给出了成功的结果,但是当我从我的离子应用程序调用时却没有。 我对离子很新,所以不太确定发生了什么事可以帮助我吗?

2 个答案:

答案 0 :(得分:2)

您没有向我们展示您apiUrl的样子,但是根据您获得的错误和网址:

POST http://localhost:8100/://localhost:8000/api/login 404 (Not Found)

听起来你正试图在url中使用相对路径。您需要提供完整的URL,否则Angular将在前面应用localhost。

所以你的apiUrl应该像:

apiUrl = 'http://localhost:8000/api/';

其中type在这种情况下为login

您也不需要在请求中使用JSON.stringify:)

答案 1 :(得分:1)

尝试像这样提供requestUrl。

let requestURl = apiUrl+type;
this.http.post(
          requestURl,
          JSON.stringify(credentials),
          { headers: headers }
      ).subscribe(res=>{
        resolve(res.JSON());
      }),(err)=>{
        reject(err);
      }
    });

因为你提供了相对url,angular将无法选择它,它会在路径前添加localhost://