我正在忙于一个小型网络应用程序,我试图使用RestService发布到API,但仍然收到400错误请求。当我使用Insomnia Rest客户端完成相同的帖子时,我得到了一个200 Ok ...任何想法我做错了什么/我能看到什么来找出发生了什么?
更新: 事实证明问题是一个不正确的标题,当我尝试添加正确的标题时,我仍然会得到一个未解决的错误... 问题延续链接Here
我的错误:
http://10.60.160.34/BRMServices/WebEnquiry/StockTake/AddToStockTake无法加载资源:服务器响应状态为400(错误请求) stockTake:1 XMLHttpRequest无法加载http://10.60.160.34/BRMServices/WebEnquiry/StockTake/AddToStockTake。 No' Access-Control-Allow-Origin'标头出现在请求的资源上。起源' http://localhost:4200'因此不允许访问。响应具有HTTP状态代码400。 main.bundle.js:47711失败:服务器错误
我的代码:
股票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的submitStockTake函数:
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'));
}
答案 0 :(得分:0)
这表明你不是两种方式都发布相同的东西 - 也许是不同的请求标题? Perhpas运行RestClient的浏览器已经过身份验证?检查网络选项卡中的实际请求,并检查URL,标题,身份验证等 - 必须有所不同。
答案 1 :(得分:0)
调查事情......在这里:angular.io http
看起来你不应该再串(如果)了,所以试试:
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'));
}
答案 2 :(得分:0)
You should authorise the header "Authorization" on your server side
答案 3 :(得分:0)
我也遇到过这个问题并通过删除代理解决了这个问题。请检查您是否设置了任何代理。