Ilkin Rufullayev
Ilkin Rufullayev

Reputation: 29

Jquery Ajax doesn't pass data to action method

Here is my AJAX method:

function SendMessage(contactId, e) {
        e.preventDefault();
        var content = $("#text").val();

        const dataObject = {
            contactId: contactId,
            content: content
        }

        console.log(dataObject)

        $.ajax({
            url: "/Admin/Contact/Reply",
            type: "POST",
            data: dataObject,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (res) {
                if (res != null) {
                    Swal.fire(
                        'Sent successfully!',
                        '',
                        'success'
                    )
                }
            }
        })
    }

Data comes from :

<form method="post">
    <input type="text" placeholder="Reply to sender"
            name="Content" required="" id="text" />
    <input type="submit" value="Send"
           onclick="SendMessage(@item.Id,event)" />
</form>

But AJAX doesn't pass data to action method:

enter image description here

I am sure data is there, as it appears in console :

enter image description here

How can I send data to action method, any idea? Thanks.

Upvotes: 1

Views: 483

Answers (1)

Victor
Victor

Reputation: 8915

Did you try to use default content format?

Remove contentType: "application/json; charset=utf-8", to use the default value. The default is: application/x-www-form-urlencoded; charset=UTF-8

If will not work try to clean a browser cache.

Upvotes: 1

Related Questions