oauth - angular2 http.post method throws typeerror{} exception -
i tried change existing angularjs library angular2 need. http.post method in below code throws typeerror {} exception. please 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 inside"); var rparameters = (result).split("&"); ..... } catch(exception){ console.log(exception) }
you should try 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 inside"); var rparameters = (result).split("&"); (...) });
here comments have on code:
- the second parameter of
post
method should string corresponding payload not callback. see headers want send url-encoded form, need create own - the
try catch
isn't necessary since executing http asynchronous , errors can "catched" within second parameter (another callback) ofsubscribe
method. - you don't need @ promise. http, angular2 uses observables under hood. target asynchronous processing well.
after fixing of this, think won't have error anymore...
hope helps you, thierry
Comments
Post a Comment