【发布时间】:2019-09-18 03:56:27
【问题描述】:
在这里,我正在尝试使用 Angular 4 捕获一些数据并将其保存到 DB(发布方法)。当单击确认按钮时出现类似 CORS 策略阻止的错误:对预检请求的响应不' t 通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。
当我尝试邮递员时,我得到了输出。所以我确认我的 Web APi 工作正常。实际上我是角度方面的新手,所以我认为我的问题在于打字稿或角度服务。我想要这样的输出。
{
"Header": {
"UserID": 6,
"created": "February 4, 2016 10:13:00",
....etc........
},
"detail": [
{
"ShopID": 1,
"ItemID": 126,
.....etc.........
},
{
"ShopID": 1,
"ItemID": 127,
.....etc.........
}
]
}
这是我的打字稿文件
stockitems: IStockCountHeaders[] = [];
items: IStockCountHeaders;
stockdetail: IStockdetails[] = [];
addItems(value: any) {
this.items = new IStockCountHeaders(this.userid, this.created, t this.confirm,this.shopid,
value.ItemID, value.PackingTypeID, value.ItemCode, value.ItemDescription,
value.PackingtypeName,
value.Stock,
);
this.stockitems.push(this.items);
}
onclick(){
this._enqService.CatchStockDetailHeader(this.stockitems)
.subscribe(value => {
if (typeof value !== 'undefined' && value != null) {
value.forEach(items => {
this.stockitems.push(this.items)
});
}
},
error => {
console.error(error);
this.statusMessage = "Problem with the service.Please try again after sometime";
});
}
这是我的 angularservice.ts 文件
CatchStockDetailHeader(stock: any)
: Observable<IStockCountHeaders[]> {
let IStockCounts = stock;
console.log(IStockCounts)
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
headers.append('userid', IStockCounts.userid);
headers.append('created', IStockCounts.created);
headers.append('.CompanyID', IStockCounts.CompanyID);
headers.append('modified', IStockCounts.modified);
headers.append('modifieduserid', IStockCounts.modifieduserid);
headers.append('confirm', IStockCounts.confirm);
return this._http.post('http://localhost:3071/api/Stockcountheader/' + 'Stock', IStockCounts, options)
.map((response: Response) => <IStockCountHeaders[]>response.json())
.catch(this.handleError);
}
startup.cs 文件
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(WebAPIService.Startup))]
namespace WebAPIService
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
}
}
我的 API
public class StockClass
{
public spGetNewStockCountHeader_Result header { get; set; }
public List<spGetNewStockCountDetails_Result> detail { get; set; }
}
public class StockcountheaderController : ApiController
{
private adminv2Entities enqentities = new adminv2Entities();
HttpSessionState session = HttpContext.Current.Session;
[HttpPost]
public IHttpActionResult Stock([FromBody] StockClass obj)
{
spGetNewStockCountHeader_Result stockheader = new spGetNewStockCountHeader_Result();
int schid = enqentities.spGetNewStockCountHeader(obj.header.UserID, obj.header.created, obj.header.CompanyID, obj.header.modified, obj.header.modifieduserid, obj.header.confirm, obj.header.ShopId).FirstOrDefault().Schid;
foreach (var Stockobject in obj.detail)
{
enqentities.spGetNewStockCountDetails(Stockobject.ShopID, Stockobject.ItemID, Stockobject.PackingTypeID, Stockobject.Itemcode, Stockobject.Itemdescription, Stockobject.PackingtypeName, Stockobject.Stockcount, schid);
}
return Ok();
}
public StockClass getStock()
{
StockClass obj = new StockClass();
spGetNewStockCountHeader_Result tmphd = new spGetNewStockCountHeader_Result();
obj.header = tmphd;
List<spGetNewStockCountDetails_Result> tmplist = new List<spGetNewStockCountDetails_Result>();
spGetNewStockCountDetails_Result tmp = new spGetNewStockCountDetails_Result();
tmplist.Add(tmp);
obj.detail = tmplist;
return obj;
}
【问题讨论】:
-
允许来自您的资源的 CORS。该错误正是在说明您需要做什么。转到您正在访问的 API 并允许来自后端的 CORS
-
@Nandan 我怎样才能分配 cors?
-
您使用的是哪个后端?可以分享一下 API sn-p
-
@Nandan Web API(实体框架)
-
你能分享我/api/Stockcountheader/代码
标签: angular typescript angular-services angular4-httpclient angular-json