使用DotNetNuke DnnApiController我无法使用$ http.post命令检索发布数据,但是.ajax可以运行

时间:2018-09-01 21:11:08

标签: angularjs ajax api post dotnetnuke

使用angular,我试图使用$ http.post将一些发布数据发送到DnnApiController:

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);

这是我的DnnApiController方法,被调用:

[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)为空,而.ajax方法在所有情况下都可以正常工作 我在$ http方法中缺少什么?

1 个答案:

答案 0 :(得分:0)

我不确定这是否可以作为答案,但它是否有效:

$http(
    {
        url: "/DesktopModules/AdvancedProductSearchAPI/API/AdvancedProductSearchApi/DoAdvancedSearchANG",
        data: JSON.stringify(searchTerms),
        method: "post",
        headers: {
            "ModuleId": moduleId,
            "TabId": tabId,
            "RequestVerificationToken": rvtoken,
            'Content-Type': 'application/json'
        }
    }).success(onSuccess).error(onFailure);

$http.post("/DesktopModules/AdvancedProductSearchAPI/API/AdvancedProductSearchApi/DoAdvancedSearchANG",
    JSON.stringify(searchTerms),
    {
        headers: {
            "ModuleId": moduleId,
            "TabId": tabId,
            "RequestVerificationToken": rvtoken,
            "Content-Type': 'application/json"
        }
    }).success(onSuccess).error(onFailure);

API:

//this method works with AJAX
[HttpPost]
[AllowAnonymous]
[DotNetNuke.Web.Api.ValidateAntiForgeryToken]
public string DoAdvancedSearchAJAX([FromBody] string advancedSearchItems)
{
    IList<SearchTerm> SearchTerms = JsonConvert.DeserializeObject<List<SearchTerm>>(advancedSearchItems);
    return JsonConvert.SerializeObject(SearchTerms);
}

//This method works with AngularJS $http.post method
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public string DoAdvancedSearchANG(IList<SearchTerm> SearchTerms)
{

    //IList<SearchTerm> SearchTerms = JsonConvert.DeserializeObject<List<SearchTerm>>(advancedSearchItems);
    return JsonConvert.SerializeObject(SearchTerms);
}

我认为我正在尝试的所有事情(例如,AngularJs $http.post() does not send data)都是针对较旧版本的Angular进行的,我使它变得比所需的更加复杂。