How do I display the string message from BadRequest(message) using Ajax

I have my API Controller

public ActionResult<Campaigns> AddCampaign([Bind("Name, Venue, AssignedTo, StartedOn, CompletedOn")] Campaigns campaigns)
            if (ModelState.IsValid)
                if (CampaignNameExists(campaigns.Name))
                    throw new Exception("A campaign with that name already exists!");
                    throw new Exception("User assigned to this Campaign does not exist!");

                var isAdmin = _accessCheck.AdminCheck(campaigns.AssignedTo);

                //Check if user is Admin
                if(isAdmin.IsAdmin == 1)
                    throw new Exception("Admins cannot be assigned to campaigns");

                //Send User details to Repo for insertion to DB
                if (condition)
                    return result
                    throw new Exception("Could not add campaign");
                throw new Exception("Model State is not valid");
        catch (Exception ex)
            var message = ex.Message;
            return BadRequest(message);


    $(document).ready(function () {

        $("#createBtn").click(function () {
            let data = {
                "Name": $('#Name').val(),
                "Venue": $('#Venue').val(),
                "AssignedTo": $('#AssignedTo').val(),
                "StartedOn": $('#StartedOn').val(),
                "CompletedOn": $('#CompletedOn').val()                
                type: "POST",
                url: "http://localhost:25733/api/Admin/Campaign/Add",
                data: JSON.stringify(data),
                contentType: "application/json",
                success: function () {
                    document.getElementById("campDivSuccess").style.display = 'block'
                error: function (response) {
                    document.getElementById("campDivFail").style.display = 'block'
                    let fail = '<p>'+ response +'</p>'

Which is supposed to add the BadRequest(string) as Paragraph tag in this div

<div class="alert alert-dismissible alert-danger" id="campDivFail" style="display: none">
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>

Which it’s doing but as [object] [Object] and not as the string which I assigned to the BadRequest, like say “Campaign Could not be assigned”. I searched a lot but could not find the solution. Am I using response wrong? Is there another function that does this?


I found the correct documentation. The content I was looking for in ‘response’ was “responseText”. I just had to use response.responseText in the paragraph. SOURCE: