邮寄请求适用于邮递员,但不适用于我的代码

时间:2018-03-29 10:05:53

标签: angular http

reg.ts

var data = JSON.stringify({
  "name": {
    "value": this.registerForm.value.email
  },
  "mail": {
    "value": this.registerForm.value.email
  },
  "pass": {
    "value": this.registerForm.value.password
  },
  "field_user_type": {
    "value": 'normal' // |  venue|celebrity|event_management//this.userType
  }
  });

  let header=new Headers({'Content-Type': 'application/json'});
  header.append("cache-control", "no-cache");

  this.http.post('..................../user/register? 
  _format=json',data, {headers: header}).
   subscribe(res=>{
   this.output=res.json()
   if(this.output) {
     this.navCtrl.setRoot(AgreementPage)
   }
  },
  err=>{
   console.log(err)
   }) 

我可以用邮递员发送帖子,并成功获得回复,但当我发出如上所示的帖子请求时收到错误消息

  

“缺少X-CSRF-Token请求标头”

2 个答案:

答案 0 :(得分:0)

我认为问题出在您的中间件中。它是由Cross-site request防御measures引起的。

因此,您需要在标头中使用特定标记,或者,在中间件中禁用CORS检查。

答案 1 :(得分:0)

您忘记创建RequestOptions对象。而不是直接传递标题尝试创建一个对象并将其传递给post方法参数



let header = new Headers({
  'Content-Type': 'application/json'
});
header.append("cache-control", "no-cache");
let options = new RequestOptions({ headers: header });

this.http.post('..................../user/register? 
    _format = json ',data, options).
    subscribe(res => {
        this.output = res.json()
        if (this.output) {
          this.navCtrl.setRoot(AgreementPage)
        }
      },
      err => {
        console.log(err)
      })