Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular2 http.post method throws typeerror{} exception

I tried to change the existing angularjs library to angular2 for my need. http.post method in the below code throws TypeError {} as exception. Someone please help as i am stuck on this.

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)
      }
like image 245
AishApp Avatar asked Feb 13 '26 10:02

AishApp


1 Answers

You should try something like that:

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("&");
        (...)
      });

Here are the comments I would have on your code:

  • The second parameter of the post method should be a string corresponding to the payload not a callback. I see from your headers that you want to send url-encoded form, so you need to create it by your own
  • The try catch isn't necessary since executing an HTTP is asynchronous and errors can be "catched" within the second parameter (another callback) of the subscribe method.
  • You don't need at all a promise. For HTTP, Angular2 uses observables under the hood. They target asynchronous processing as well.

After fixing all of this, I think that you won't have error anymore...

Hope it helps you, Thierry

like image 53
Thierry Templier Avatar answered Feb 15 '26 20:02

Thierry Templier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!