使用angular,我正在尝试发送一些post数据到dnapicontroller,使用$http.post:
findit: function(onSuccess, onFailure,searchTerms) {
alert(JSON.stringify(searchTerms));
const rvtoken = $("input[name='__RequestVerificationToken']").val();
$http({
cache: false,
dataType: 'json',
url: "/DesktopModules/AdvancedProductSearchAPI/API/AdvancedProductSearchApi/DoAdvancedSearch",
method: "Post",
headers: {
"ModuleId": moduleId,
"TabId": tabId,
"RequestVerificationToken": rvtoken
},
data: { "": JSON.stringify(searchTerms) }
}).success(onSuccess).error(onFailure);
----.ajax方法----
findit: function(onSuccess, onFailure,searchTerms) {
alert(JSON.stringify(searchTerms));
const rvtoken = $("input[name='__RequestVerificationToken']").val();
$.ajax({
cache: false,
dataType: 'json',
url: "/DesktopModules/AdvancedProductSearchAPI/API/AdvancedProductSearchApi/DoAdvancedSearch",
method: "Post",
headers: {
"ModuleId": moduleId,
"TabId": tabId,
"RequestVerificationToken": rvtoken
},
data: { "": JSON.stringify(searchTerms) }
}).success(onSuccess).error(onFailure);
更新2018/09/02
我试过这个:
$http.post("/DesktopModules/AdvancedProductSearchAPI/API/AdvancedProductSearchApi/DoAdvancedSearch3",
searchTerms,
{
headers: {
"ModuleId": moduleId,
"TabId": tabId,
"RequestVerificationToken": rvtoken
}
}).success(onSuccess).error(onFailure);
下面是调用的dnapicontroller方法:
[AllowAnonymous]
[DotNetNuke.Web.Api.ValidateAntiForgeryToken]
public string DoAdvancedSearch([FromBody] string advancedSearchItems)
{
IList<SearchTerm> SearchTerms = JsonConvert.DeserializeObject<List<SearchTerm>>(advancedSearchItems);
return JsonConvert.SerializeObject(SearchTerms);
}
public string DoAdvancedSearch2([FromBody] IList<SearchTerm> SearchTerms)
{
//IList<SearchTerm> SearchTerms = JsonConvert.DeserializeObject<List<SearchTerm>>(advancedSearchItems);
return JsonConvert.SerializeObject(SearchTerms);
}
或
[HttpPost]
[AllowAnonymous]
[DotNetNuke.Web.Api.ValidateAntiForgeryToken]
[Route("doadvancedsearch3")]
public string DoAdvancedSearch3(IList<SearchTerm> SearchTerms)
{
//IList<SearchTerm> SearchTerms = JsonConvert.DeserializeObject<List<SearchTerm>>(advancedSearchItems);
return JsonConvert.SerializeObject(SearchTerms);
}
所有API都是从$http.post命令,具体取决于我调用的api。但是$http.post方法会导致api的参数(advancedSearchItems)为null,而.ajax方法在所有情况下都可以正常工作