Angular2:当发布到API时,继续得到一个400错误的请求错误,但是当使用RestClient时,我得到了一个200 Ok

Angular2:当发布到API时,继续得到一个400错误的请求错误,但是当使用RestClient时,我得到了一个200 Ok,rest,angular,Rest,Angular,我正忙于一个小的web应用程序,我正试图使用RestService发布到API,但一直收到400个错误请求。当我使用失眠症Rest客户端发布完全相同的帖子时,我得到了一个200 Ok。。。你知道我做错了什么/我可以看看什么来找出发生了什么吗 更新: 原来问题是标题不正确,在尝试添加正确的标题时,仍然存在未解决的错误。。。 问题延续链接 我的错误: 加载资源失败:服务器响应状态为400(错误请求) 盘点:无法加载1 XMLHttpRequest。请求的资源上不存在“Access Control A

我正忙于一个小的web应用程序,我正试图使用RestService发布到API,但一直收到400个错误请求。当我使用失眠症Rest客户端发布完全相同的帖子时,我得到了一个200 Ok。。。你知道我做错了什么/我可以看看什么来找出发生了什么吗

更新: 原来问题是标题不正确,在尝试添加正确的标题时,仍然存在未解决的错误。。。 问题延续链接

我的错误:

加载资源失败:服务器响应状态为400(错误请求) 盘点:无法加载1 XMLHttpRequest。请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“”。响应的HTTP状态代码为400。 main.bundle.js:47711失败:服务器错误

我的代码:

stock-take.component.ts:

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

import { RestService } from '../../services/rest.service';

import { StockTakeModel } from '../../models/stock-take.model';

@Component({
  selector: 'stock-take',
  templateUrl: './stock-take.component.html',
  styleUrls: ['./stock-take.component.css']
})
export class StockTakeComponent implements OnInit {
    stockTakeForm: FormGroup;
    stockTakeModel: StockTakeModel;

  constructor(private fb: FormBuilder, private restService: RestService) { 
    this.stockTakeForm = fb.group({
        'SheetNo':['', Validators.required],
        'BinNo':['', Validators.required],
        'BarCode':['', Validators.required],
        'Quantity':['', Validators.required]
    });
    }

    doStockTake(val: any) {
        //console.log("val:" + JSON.stringify(val));
        //this.stockTakeModel = new StockTakeModel(0, 0, val[Object.keys(val)[2] '', val[Object.keys(val)[0]], val[Object.keys(val)[1]], val[Object.keys(val)[3]], 0);
        this.stockTakeModel = val;
    this.stockTakeModel.StockTakeID = '0';
    this.stockTakeModel.IDKey = '0';    
    this.stockTakeModel.ProductCode = '';
    this.stockTakeModel.PackSize = '0';
    console.log(this.stockTakeModel);
    console.log(JSON.stringify(this.stockTakeModel));
    this.submitStockTake(this.stockTakeModel);
    }

  submitStockTake(stockTakeModel: StockTakeModel) {
      //console.log(stockTakeModel);

    this.restService.postStockTake(stockTakeModel)
    .subscribe(
    (res) => {
        console.log(res);
    },
    (res) => {
        console.log("failure: " + res);
    }
    );
    this.stockTakeForm.reset();
  }

  ngOnInit() {

  }

}
从rest.service.ts提交tocktake函数:

postStockTake(stockTakeModel: StockTakeModel) : Observable<Response> {
      console.log(JSON.stringify(stockTakeModel));

        return this.http.post(this.API_URL + "StockTake/AddToStockTake", JSON.stringify(stockTakeModel), this.headers)
        .map((res: Response) => res.json())
        .catch((error: any) => Observable.throw(error.json().error || 'server error'));
  }
盘点后(盘点模型:盘点模型):可观察{
log(JSON.stringify(stockTakeModel));
返回this.http.post(this.API_URL+“StockTake/AddToStockTake”、JSON.stringify(stockTakeModel)、this.headers)
.map((res:Response)=>res.json())
.catch((error:any)=>Observable.throw(error.json().error | |“服务器错误”);
}

这表明您并没有从两个方面发布相同的内容-可能是不同的请求头?是否运行RestClient的浏览器已通过身份验证?检查“网络”选项卡中的实际请求,并检查URL、标题、身份验证等-某些内容必须有所不同。

查看内容。。。在这里:

看来你不应该(再)严格化了,所以试试:

postStockTake(stockTakeModel:string):可观察{
let headers=新的头({'Content-Type':'application/json'});
let options=newrequestoptions({headers:headers});
返回this.http.post(this.API_URL+“StockTake/AddToStockTake”,{stockTakeModel},options)
.map((res:Response)=>res.json())
.catch((error:any)=>Observable.throw(error.json().error | |“服务器错误”);
}

您应该在服务器端授权标题“Authorization”

我也遇到了这个问题,并通过删除代理解决了这个问题。请检查是否设置了任何代理。

在“网络”选项卡下,我在请求下遇到以下错误:服务器在处理请求时遇到错误。异常消息为“传入消息具有意外的消息格式”“Raw”。操作的预期消息格式为“Xml”、“Json”。这可能是因为尚未在绑定上配置WebContentTypeMapper。有关详细信息,请参阅WebContentTypeMapper的文档。“。有关详细信息,请参阅服务器日志。异常堆栈跟踪为:。。。所以我猜这意味着发送的JSON有问题吗?发送的JSON是:{“SheetNo”:“a11”,“BinNo”:“b8”,“BarCode”:222,“Quantity”:2,“StockTakeID”:“0”,“IDKey”:“0”,“ProductCode”:“PackSize”:“0”}我看到的唯一两件可能是问题的事情是空字符串ProductCode?而且数值没有被“”包围?不确定这是否是一个问题…所以它似乎是作为内容类型发送的:text/plain。。。你知道如何在angular2 post请求中正确添加头吗?看看吧,我试着这么做,但它在chrome控制台中给了我一个net::err连接重置。。。代码在上面的帖子里。啊,好的,现在我明白了。你能给你的问题添加链接吗,这样人们就不会像我一样困惑和反应;)谢谢@user2094257我取消删除了我的答案并进行了更新,尝试上面的方法,看看会发生什么:)我仍然得到完全相同的错误。。。这很不寻常。。。我想知道这可能是什么。。。也许这可能是相关的…现在看看你之前的问题。。。无论如何,您应该在post请求中使用
this.options
而不是
this.headers
。您的RESTAPI实际上接受POST请求而不是GET?只是检查一下;)不,它接受get请求,但不接受post。我以前尝试过使用带有requestOptions的get,但也遇到了同样的问题。抱歉搞混了……好吧,你必须确保你的其他人都在接受这篇文章,而不是被拒绝。如果rest接受get而不是post,那当然会导致错误。并将
this.headers
改为
this.options
。当您发送get请求时,rest也需要get。当你发送邮件时,其他人也必须有邮件。否则它将无法工作。当使用Rest客户端时,它可以正常工作。。。所以它不可能是API。。。我在过去使用RequestOptions处理工作get请求时遇到了完全相同的问题。这是我必须为这个应用程序做的第一个Post请求,我想不出再使用RequestOptions的任何方法了。。。
postStockTake(stockTakeModel: string) : Observable<Response> {
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

      return this.http.post(this.API_URL + "StockTake/AddToStockTake", { stockTakeModel }, options)
        .map((res: Response) => res.json())
        .catch((error: any) => Observable.throw(error.json().error || 'server error'));
  }