0

Using angular, I'm trying to send some post data to a DnnApiController using $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 method ----

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

Update 2018/09/02 I've tried this:

$http.post("/DesktopModules/AdvancedProductSearchAPI/API/AdvancedProductSearchApi/DoAdvancedSearch3",
searchTerms,
{
    headers: {
        "ModuleId": moduleId,
        "TabId": tabId,
        "RequestVerificationToken": rvtoken
    }
}).success(onSuccess).error(onFailure);

Here is my DnnApiController method that's getting called:

[AllowAnonymous]
[DotNetNuke.Web.Api.ValidateAntiForgeryToken]
public string DoAdvancedSearch([FromBody] string advancedSearchItems)
{
    IList<SearchTerm> SearchTerms = JsonConvert.DeserializeObject<List<SearchTerm>>(advancedSearchItems);
    return JsonConvert.SerializeObject(SearchTerms);
}

or

public string DoAdvancedSearch2([FromBody] IList<SearchTerm> SearchTerms)
        {
            //IList<SearchTerm> SearchTerms = JsonConvert.DeserializeObject<List<SearchTerm>>(advancedSearchItems);
            return JsonConvert.SerializeObject(SearchTerms);
        }

or

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

All apis are fired from the $http.post command depending on which api I'm calling. But the $http.post method results in a null for the api's parameter (advancedSearchItems) while the .ajax method works just fine in all cases What am I missing in the $http method?

Chris
  • 650
  • 7
  • 20
  • If you are a DNNHero.com subscriber, you can watch a tutorial on advanced angularJs implementation for DNN modules - which includes working source code project. https://www.dnnhero.com/video/advanced-angular-concepts-for-dnn-introduction-and-demonstration-of-module. I use a method where I overload angular's $httpProvider in order to keep the $http.post() method simple and consistent. Too much code to post here... That said, I am looking at your code and don't see anything obvious at the moment. – Fix It Scotty Sep 03 '18 at 22:04
  • @Chris maybe make the public string DoAdvancedSearch3(IList SearchTerms) able to receive an agnostic object instead of a IList, and once you verify the results are passing through then you adjust your input data. Also this -> data: { "": JSON.stringify(searchTerms) }, why not remove "": – alwaysVBNET Sep 07 '18 at 07:39
  • I've tried dynamic, object and a simple string. I've tried removing the "", added the object name "data", and replaced "" with "searchTerms". No go. It works great with an ajax call but never with the http.post method. – Chris Sep 07 '18 at 12:56

1 Answers1

0

I'm not sure this even qualifies as an answer but it's working:

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

or

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

I think all the things I was trying (AngularJs $http.post() does not send data, for instance) were for an older version of Angular and I was making it more complicated than it needed to be.

Chris
  • 650
  • 7
  • 20