angularjs - New to Typescript & Angular problems with Http Post -
i'm new angular , typescript , try create simple login page. i've created service in typescript invoked when user presses 'login' button. textboxes contains username , password bound correctly model, when send request backend written in c#, not hit breakpoint suspect because of format of message being sent on wire.
so using postman
, i'm able invoke service , access_token
when exporting request code in postman
nodejs variant like:
var request = require("request"); var options = { method: 'post', url: 'http://localhost:8081/login', headers: { 'postman-token': '34dd4d0f-ff16-db4f-ebae-dab945729410', 'cache-control': 'no-cache', 'content-type': 'application/x-www-form-urlencoded' }, form: { username: 'test', password: 'test', grant_type: 'password' } }; request(options, function (error, response, body) { if (error) throw new error(error); console.log(body); });
and typescript code
login(username: string, password:string) : observable<boolean> { var headers = new headers(); headers.append('content-type', 'application/x-www-form-urlencoded') var content = json.stringify({ username: username, password: password, grant_type: this.grant_type }); return this.http.post(this.authenticationendpoint, content, {headers: headers}) .map((response:response) => { let token = response.json() && response.json().token; if(token){ //this.token = token; localstorage.setitem('user', json.stringify({username: username, token:token})); return true; } return false; }); }
this results in error in visual studio code, says:
i'm not sure how should interpret error message, since method in webservice not invoked i'm pretty sure has http headers or format of http post.. ideas?
using urlsearchparams
body of request , angular automatically set content type application/x-www-form-urlencoded
import { urlsearchparams } "@angular/http" let body = new urlsearchparams(); body.set('username', username); body.set('password', password); ..... this.http.post(this.authenticationendpoint, body).map(..)
Comments
Post a Comment