1

I cannot get DotNetNuke to execute the backend code from my JQuery Ajax function. I have the following JQuery code on my View.ascx file

I did try to change the URL to View.ascx/DeleteReviewData but no luck.

function deleteReview(ReviewID){
    var ReviewIDToDelete = ReviewID;
    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "https://dnndev.me/Product-View/DeleteReviewData",
        data: "{'deleteReviewID': '"+ ReviewIDToDelete +"'}",
        datatype: "json",
        success: function (data) {
            alert("Delete successfull");
        },
        error: function (error) {
            alert(error);
        }
    });
}

This is my back-end code which doesn't get executed on the View.ascx.cs file:

[System.Web.Services.WebMethod]
    public static void DeleteReviewData(int deleteReviewID)
    {
        try
        {
            //Deletes a review from the database
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SiteSqlServer"].ToString()))
            {
                connection.Open();

                using (SqlCommand command = new SqlCommand($"delete from ProductReviews where ReviewID = {deleteReviewID}"))
                {
                    command.Connection = connection;
                    command.ExecuteNonQuery();
                }
                connection.Close();
            }
        }
        catch(Exception ex)
        {
            throw;
        }
    }

If I should use MapHttpRoute. Does someone have an example, please?

I looked at the following post, but I am not sure about using RouteConfig.cs and extra headers and etc: https://www.dnnsoftware.com/answers/execute-an-action-by-calling-an-ajax-post

I currently get no Console errors. It goes to the success section. When I hover over Type, ContentType or any one of those while debugging it says not defined. See example below. The site is using JQuery 01.09.01

enter image description here

2nd image enter image description here

UPDATE

I have changed the URL which now gives me a 404 error: url: $.fn.GetBaseURL() + 'DesktopModules/ProductDetailedView/DeleteReviewData'

I also tried this URL path with adding API API/DeleteReviewData , but I get a [object Object] error as it shows a 404 error in the console.

Tig7r
  • 525
  • 1
  • 4
  • 21
  • 1
    Have you checked the console for errors? There are thousands of reasons for a request to be failing. – Rory McCrossan Mar 25 '19 at 10:32
  • @RoryMcCrossan, I checked and there are no Console Errors. What is strange is when you hover over type,ContentType or any other parameter it says undefined, but clicking on them will take me to the JQuery 1.9.1 file. – Tig7r Mar 25 '19 at 10:51

1 Answers1

2

This is an example:

    $.ajax({
        data: { "Id": IdToDelete },
        type: "POST",
        dataType: "json",
        url: "/DesktopModules/{API-ProjectName}/API/Main/DeleteExpenseByID"
    }).complete(function () {
        //...
    });

Api method:

    [HttpPost]
    [DnnAuthorize]
    public void DeleteExpenseByID(int Id)
    {
       //...
    }

You need to send a number so you dont need the "'" surrounding ReviewIDToDelete var.

Also check DeleteReviewData for a [POST] attribute, it seems to be a [GET] call.

Erick Lanford Xenes
  • 1,467
  • 2
  • 20
  • 34
  • Is the API-ProjectName the Default namespace or Assembly name? Also, do you have an example of the controller section? My DNN instance still can't find the URL. I looked at this article. https://bytutorial.com/blogs/dnn/how-to-implement-web-api-service-in-dnn – Tig7r Mar 25 '19 at 15:07
  • 1
    API-ProjectName should be your project name, but you can always check the names on windows explorer under DesktopModules – Erick Lanford Xenes Mar 25 '19 at 15:20
  • 1
    If you are getting 404, try to use Postman in order to do several tests – Erick Lanford Xenes Mar 25 '19 at 15:21