2017-11-10 53 views
1

几分钟前,我在最新的Ionic中创建了一个新项目。我导入了离子原生http获取返回类型错误 - 无法读取未定义的属性“匹配”

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

之后我试着发送GET请求到我的后端应用程序。

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); 
     }); 

我正在使用'ionic cordova run browser'命令。我试过在android上运行,但效果相同。

在每一种情况下,我得到了错误:

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) 

回答

1

你已经做错了。您需要设置headers的详细信息。

参见doc

get(url, parameters, headers) 

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); 
    }); 
+0

的URL我有标头:内容类型” :“应用/ JSON。但我想用登录名和密码使用基本身份验证,而不是oauth2 – Algeroth

+0

这只是doc的一个示例。请参阅:https://stackoverflow.com/a/34465070/1077309 – Sampath

1

实施例的登录请求,其中AppSignIn就像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) 
     } 
相关问题