在这里,我正在尝试使用角度 4 捕获一些数据并将其保存到数据库(发布方法)。当单击确认按钮时出现错误,例如 被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:没有“访问” -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);
}
启动.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;
}