b_900
b_900

Reputation: 149

MVC C# pass from backend to frontend

I have passed a token from a View to a function in my HomeController, and now want to perform an action on the token and return the information to the frontend. I thought that the resultData from the ajax call would be the output of GetMyData, however it is just the token that I passed in in the first place. Any help would be great, thanks.

JS

$.ajax({
    url: '/Home/GetMyData',
    type: 'POST',
    dataType: 'json',
    data: {token: token},
    success: function(resultData){
        console.log(resultData);
    }
})

Home Controller

public JsonResult GetMyData(string token)
{
    ...
    return Json('ok')
}

Upvotes: 0

Views: 1046

Answers (1)

Nikki9696
Nikki9696

Reputation: 6348

This works fine for me. Post more details if it's not working for you.

MVC

public JsonResult GetMyData(string token)
        {
            return Json("some result info");
        }

Script

<script>
    function getData()
    {
        let token = "test";
        $.ajax({
            url: '/Home/GetMyData',
            type: 'POST',
            dataType: 'json',
            data: { token: token },
            success: function (resultData) {
                console.log(resultData);
            }
        })
    }
</script>

enter image description here

Upvotes: 1

Related Questions