2

我是 Angular 4 的新手。我正在登录页面上,我必须在登录页面上发布用户信息。但我收到错误为“代码”:“rest_missing_callback_param”,“消息”:“缺少参数:电子邮件,密码”发布数据。以下是我的代码:

  import { Component, OnInit } from '@angular/core';
import {Http,Response,Headers, RequestOptions} from '@angular/http';
import { Router} from '@angular/router';
import { NgForm } from '@angular/forms';

@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {

constructor(private http:Http,private router:Router) { }
login(){
this.router.navigate(['login']);
}
     createAuthorizationHeader(headers: Headers) {
    headers.append('Authorization', 'Basic ' +
      btoa('ck_543700d9f8c08268d75d3efefb302df4fad70a8f:cs_f1514261bbe154d662eb5053880d40518367c901')); 
    headers.append("Content-Type", "application/x-www-form-urlencoded");
  }   
onSubmit(userForm:NgForm){
    console.log(userForm.value);
    let headers=new Headers();
     this.createAuthorizationHeader(headers);
const body={
 "email": userForm.value.email,
  "first_name": userForm.value.fname,
  "last_name": userForm.value.lname,
  "username": userForm.value.uname,
  "password": userForm.value.password
}
    console.log(body);
    console.log(headers);
this.http.post('https://www.colourssoftware.com/wordpress/wp-json/wc/v2/customers',body,{
        headers:headers
        })
      .subscribe(
      data => {
              console.log(data);
            },
            err => {
              console.log("ERROR!: ", err);
            }
      );

}   
  ngOnInit() {
  }

}
 <form class="animated wow slideInUp" data-wow-delay=".5s" #regForm="ngForm" (ngSubmit)="onSubmit(regForm)">
                    <input type="text" placeholder="First Name..." required=" " name="fname" ngModel>
                    <input type="text" placeholder="Last Name..." required=" " name="lname" ngModel>
                    <input type="text" placeholder="UserName..." required=" " name="uname" ngModel>
                    <h6 class="animated wow slideInUp" data-wow-delay=".5s">Login information</h6>

                    <input type="email" placeholder="Email Address" required=" " name="email" ngModel>
                    <input type="password" placeholder="Password" required=" " name="password" ngModel>
                    <div class="register-check-box">
                        <div class="check">
                            <label class="checkbox"><input type="checkbox" name="checkbox"><i> </i>I accept the terms and conditions</label>
                        </div>
                    </div>
                    <input type="submit" value="Register">
                </form>

我在哪里做错了?如何以角度 4 将数据发布到服务器?请帮我。

4

2 回答 2

0

在您提交方法中,您正在形成一个 JSON 对象,但您将其发送为application/x-www-form-urlencoded. 我的猜测是您应该将标题设置为application/json

于 2017-09-26T13:45:59.527 回答
0

我知道这是一个非常古老的问题,但我正在回答这个问题,以便其他人可能受益。

> You should use yoursite.com/wordpress/wp-json/wc/v2/users API instead
> of yoursite.com/wordpress/wp-json/wc/v2/customers

在验证用户登录或使用 WordPress REST API 注册客户时。另外,请确保您在标题中设置了有效的授权。

*** 如果您想使用 yoursite.com/wordpress/wp-json/wc/v2/customers API,那么您必须在 WordPress 中拥有具有相同用户名/密码的客户帐户,然后只有您才能作为客户进行验证使用 woocommerce api

于 2022-02-09T04:06:49.923 回答