Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oauth angular2 http.post方法引发typeerror{}异常_Oauth_Ionic Framework_Angular_Angular Promise_Ionic2 - Fatal编程技术网

Oauth angular2 http.post方法引发typeerror{}异常

Oauth angular2 http.post方法引发typeerror{}异常,oauth,ionic-framework,angular,angular-promise,ionic2,Oauth,Ionic Framework,Angular,Angular Promise,Ionic2,我试图根据需要将现有的angularjs库更改为angular2。下面代码中的http.post方法抛出TypeError{}作为异常。有人请帮忙,因为我被困在这件事上了 login() { return new Promise((resolve, reject) => { if(typeof jsSHA !== "undefined") { var signatureObj = (new OauthUtility()).createSignature("POST"

我试图根据需要将现有的angularjs库更改为angular2。下面代码中的http.post方法抛出TypeError{}作为异常。有人请帮忙,因为我被困在这件事上了

login() {
  return new Promise((resolve, reject) => {
    if(typeof jsSHA !== "undefined") {
      var signatureObj = (new OauthUtility()).createSignature("POST", this.magentoOptions.baseUrl+"/oauth/initiate", this.oauthObject, {oauth_callback: "http://localhost/callback"}, this.magentoOptions.clientSecret, null);                                   
      let headersInitiate = new Headers();
      headersInitiate.append('Authorization',signatureObj.authorization_header);
      headersInitiate.append('Content-Type','application/x-www-form-urlencoded');
      let url = this.magentoOptions.baseUrl + "/oauth/initiate";
      let callback = "oauth_callback=http://localhost/callback";

      try{
        this.http.post(url, callback,{headers: headersInitiate})
         .subscribe(
          (result) => {
          console.log("i am inside");
          var rParameters = (result).split("&");
                            .....
      }
      catch(Exception){
        console.log(Exception)
      }

你应该试试这样的方法:

var signatureObj = (new OauthUtility()).createSignature("POST", 
     this.magentoOptions.baseUrl+"/oauth/initiate", this.oauthObject,
     {oauth_callback: "http://localhost/callback"},
     this.magentoOptions.clientSecret, null);    let headersInitiate = new Headers();

headersInitiate.append('Authorization',
          signatureObj.authorization_header);
headersInitiate.append('Content-Type',
          'application/x-www-form-urlencoded');
let url = this.magentoOptions.baseUrl + "/oauth/initiate";

let payload = ' ... ';
this.http.post(url, payload,{headers: headersInitiate})
    .subscribe(
      (result) => {
        console.log("i am inside");
        var rParameters = (result).split("&");
        (...)
      });
以下是我对您的代码的评论:

  • post
    方法的第二个参数应该是对应于有效负载的字符串,而不是回调。我从您的标题中看到,您希望发送url编码的表单,所以您需要自己创建它
  • try-catch
    不是必需的,因为执行HTTP是异步的,可以在
    subscribe
    方法的第二个参数(另一个回调)中“捕获”错误
  • 你根本不需要承诺。对于HTTP,Angular2使用引擎盖下的可观察对象。它们也以异步处理为目标
在修复了所有这些之后,我想你不会再有错误了

希望它能帮助你,
Thierry

我发现,即使在继续执行上述所有步骤后,我仍会卡住。完整的解决方案如下。 按照Thierry的建议,移除试扣块并承诺。 在构造函数中使用http的依赖项注入,如下定义http

import {Http,HTTP_PROVIDERS,Headers} from 'angular2/http';
import {Injector} from "angular2/core";
 constructor() {

        var injector = Injector.resolveAndCreate([HTTP_PROVIDERS]);
        this.http = injector.get(Http);
}

谢谢@Thierry的回复。我在POST方法中通过变量回调传递了参数。let callback=“oauth_callback=”;很抱歉,我在以前的代码中错过了它。我没有在subscribe方法的第二个参数中捕捉到我的错误。这就是为什么我在文章中加入了try-catch。也许我会尝试排除承诺。我试图取消承诺,但我面临着这个问题。问题的原因是http未定义。我也包括了@Injectable,但问题仍然是一样的。请帮助您是否在
引导
函数的第二个参数中添加了
HTTP\u提供程序
?非常感谢。我能够进入函数:)为什么不从构造函数中注入
Http
?在
bootstrap
函数或使用
HTTP
对象的组件中注册HTTP_提供程序时,可以执行此操作。这样您就不需要显式地引用
注入器
…我正在为我的ionic2应用程序创建anguar2库。因为这是一种服务,所以在构造函数中注入的Http不起作用。它仍然没有定义。这就是我的代码出错的原因。当我使用injector注入时,它工作正常。您是否将
@Injectable
装饰器添加到您的服务中?是的。但它不起作用。我还尝试在构造函数中使用@Inject(HTTP)HTTP:HTTP。我不知道它为什么不起作用。我应该;-)您是否使用了这个:
bootstrap(AppComponent,[HTTP_PROVIDERS])