【问题标题】:Post request works in postman but not in my code发布请求在邮递员中有效,但在我的代码中无效
【发布时间】:2018-03-29 22:15:33
【问题描述】:

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 请求头丢失”

【问题讨论】:

  • 可以分享后端代码和使用的技术吗?

标签: angular http


【解决方案1】:

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

因此,您要么在标头中需要一个特定的令牌,要么在中间件中禁用 CORS 检查。

【讨论】:

    【解决方案2】:

    您忘记创建 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)
          })

    【讨论】:

      猜你喜欢
      • 2020-07-26
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 2021-01-14
      • 1970-01-01
      • 2018-03-29
      • 1970-01-01
      相关资源
      最近更新 更多