angular - 使用angular在HTTP调用中获取表单数据

标签 angular typescript ionic-framework ionic2 angular2-routing

This is how the authentication is supposed to be.

发布https://DomainName/api/v1/authentication?username= {用户名}&密码={密码}.. 这是 typescript 认证调用代码

authenticate(url, payload){
let username: string = 'admin';
let password: string = 'password';
let headers: Headers = new Headers();
  headers.append("Authorization", "Basic " + btoa(username + ":" + password)); 
  headers.append("Content-Type", "application/json");
  headers.append("Access-Control-Allow-Origin", "*");
return this.http.post(server + url + addon, payload, { headers:headers });

这是表单数据的代码

    // Getting data info from signing form
loginForm = new FormGroup({
username: new FormControl(null, ([Validators.required, Validators.minLength(5)])),
password: new FormControl(null, ([Validators.required, Validators.minLength(5)])),
})

用户数据

// user data 
loginData={
username:'',
    password:'',
}

buildLoginData(){
    this.loginData.password = this.loginForm.controls['password'].value;
this.loginData.username = this.loginForm.controls['username'].value;
}

登录功能

    signin(){
    this.loading =this.loadingCtrl.create({
  content:"Verifying Credentials ..."
});
this.loading.present();
    this.buildLoginData();
    console.log(this.loginData);
  this.http.store('authentication', this.loginData).subscribe((response)=>{
            // console.log(response);
            // console.log("*******************************************");
            // console.log(response['username']);
            localStorage.setItem('office_ID',response['officeId']);
            localStorage.setItem('user_ID', response['userId']);
            localStorage.setItem('username', response['username']);
            this.loading.dismissAll();
            this.navCtrl.push(HomePage);
  },error=>{
            this.loading.dismissAll();
                if(error.status === 401){
                    this.toastr.messenger('Wrong Username or Password');
                }else
    this.toastr.messenger('Login Failed!');
            console.log(JSON.stringify(error));
            alert("Error : " + error );
});    
}

响应日志数据

{"_body":"{\"developerMessage\":\"Invalid authentication details were 
passed in api request.\",\"httpStatusCode\":\"401\",\"defaultUserMessage 
\":\"Unauthenticated. Please login.\",\"userMessageGlobalisationCode
\":\"error.msg.not.authenticated\",\"errors
\":[]}","status":401,"ok":false,"statusText":"Unauthorized","headers":
{"Content-Type":["application/json"]},"type":2,"url":"https//x.x.x.x:xxxx            
/xxxxxxxxx/api/v1/authentication?tenantIdentifier=default"} 

错误

 "Unauthorized" and this is because the form values are not being sent to                                                                                     
  the api as headers.

非常感谢任何帮助。谢谢。

最佳答案

你的this.http.store(url,data)
在后台添加url参数?
authentication?username={username}&password={password}..

如果不是最快的方法是自己做
const url =`authenticationusername=${this.loginData.username}&password=${this.loginData.password}`

然后你可以做
this.http.store(url,this.loginData)

或者您可以查看 URL 参数 https://angular.io/guide/http#url-parameters

关于angular - 使用angular在HTTP调用中获取表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51782242/

相关文章:

javascript - 无法以 Angular 正确设置某些 HTTP header 键值对

javascript - Angular2 Typescript如何从对象内部检索数组

css - Ionic 4,如何降低ionic-segment-button的高度

javascript - 使用 Ionic/AngularJS 和 Cordova 为 native 和 Web 应用程序使用相机

angular - 使用 Angular2/Ionic2 标记输入

javascript - 如何在模板中运行可观察函数?

angular - 如何使用angular 7 Material 图标按钮进行星级评分?

angular - 在不指定类型的情况下实例化 TypeScript 泛型类

javascript - 如何使用组件将动态 HTML 加载到 DIV 中? Angular 5

android-studio - 无效的可执行目录 : . ./emulator