代码之家  ›  专栏  ›  技术社区  ›  Chris

使用DotNetNuke dnapicontroller,我无法使用$http.post但是,ajax是有效的

  •  0
  • Chris  · 技术社区  · 6 年前

    使用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方法在所有情况下都可以正常工作

    1 回复  |  直到 6 年前
        1
  •  0
  •   Chris    6 年前

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

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

    //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