简体   繁体   中英

ionic native http get return typeError - Cannot read property 'match' of undefined

Few minutes ago I created a new project in the newest Ionic. I imported

import { HTTP } from '@ionic-native/http'

and after that I tried to send GET request to my backend application.

meetingsUrl: 'http://localhost:8080/test/all';
 this.http.useBasicAuth('login','password');
    this.http.get(this.meetingsUrl,{}, {'Content-Type':'application/json'})
      .then(data => {
        console.log(data);
      })
      .catch(error => {
        console.log(error);
      });

I'm using 'ionic cordova run browser' command. I tried run on android, but it was the same effect.

In every case I got error:

TypeError: Cannot read property 'match' of undefined
    at getMatchingHostHeaders (advanced-http.js:118)
    at getMergedHeaders (advanced-http.js:126)
    at Object.get (advanced-http.js:210)
    at callCordovaPlugin (plugin.js:110)
    at plugin.js:136
    at util.js:22
    at new t (polyfills.js:3)
    at tryNativePromise (util.js:21)
    at getPromise (util.js:29)
    at wrapPromise (plugin.js:119)

You have done it wrong. You need to set the headers details.

See the doc

get(url, parameters, headers)

Example from the doc :

    this.http.get('https://google.com/', {
      id: 12,
      message: 'test'
    }, { Authorization: 'OAuth2: token' }).then(data => {
       console.log(data.status);
       console.log(data.data); // data received by server
       console.log(data.headers);
  })
  .catch(error => {
      console.log(error.status);
      console.log(error.error); // error message as string
      console.log(error.headers);
  });

Example for LogIn request, where AppSignIn is the URL like http://www.example.com/

public signInRequest(login:Login):Promise<OAuth> {
        let params:URLSearchParams = new URLSearchParams();
        params.set('userId', login.userId.toString());
        params.set('password', login.password.toString());
        let requestOption:RequestOptionsArgs = {
            search: params
        };
        return this.http
            .get(AppSignIn, requestOption)
            .toPromise()
            .then(this.extractData)
      }

Try:

doLogin(){

   let base64Auth = this.http.getBasicAuthHeader("username", "password"); // return like {Authorization: "Basic base64UsernameAndPassword" } 

   this.http.get("url", {}, base64Auth).then((res)=>{
      //success callback 
      console.log(res); 
   }).catch((err)=>{
      //error callback 
      console.log(err); 
   }); 

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM