#jquery #ajax #post #controller #fetch
Вопрос:
У меня есть базовый контроллер HttpPost, который принимает строку, которая вызывается из запроса на извлечение jquery
[HttpPost]
[AntiforgeryTokenCustomValidator]
public bool SaveRetailers([FromBody]string RetailersIds)
{
try
{
if (string.IsNullOrEmpty(RetailersIds))
return false;
var RetailersList = JsonConvert.DeserializeObject<List<string>>(RetailersIds);
var customerId = Sitecore.Security.Accounts.User.Current?.Profile?.ProfileItemId;
if (RetailersIds.Any())
{
foreach (var retailerId in RetailersList)
{
Guid retailerGuid = new Guid(retailerId);
var retailer = _ieBusinessEntities.Retailers.FirstOrDefault(x => x.ID == retailerGuid);
if (retailer == null)
continue;
_ieBusinessEntities.CustomersPreferredRetailers.Add(new CustomersPreferredRetailer
{
ID = Guid.NewGuid(),
CustomerId = customerId,
RetailerId = retailer.ID,
});
}
_ieBusinessEntities.SaveChanges();
}
return true;
}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Error(ex.Message, ex, this);
return false;
}
}
}
(retailrsIds-это массив)
const postData = (retailerIds) => {
return fetch(saveUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
__RequestVerificationToken: antiforgeryToken,
},
body: JSON.stringify(retailerIds),
dataType: 'json'
})
};
Я отлаживал код, и действие контроллера выполняется, но RetailersIds
всегда равно нулю. Я пробовал переключиться body
на data
в jquery, но это тоже не работает.
Комментарии:
1. Да, я тоже попробовал следующее. retailerIds-это массив
const postData = (retailerIds) => { var data = JSON.stringify(retailerIds); return fetch(saveUrl, { method: "POST", headers: { "Content-Type": "application/json", __RequestVerificationToken: antiforgeryToken, }, data: "{RetailersIds: " data "}", dataType: 'json', })