【问题标题】:jQuery AJAX Not Calling A C# Function In ASP.NET MVCjQuery AJAX 未在 ASP.NET MVC 中调用 C# 函数
【发布时间】:2019-07-26 05:35:19
【问题描述】:

基本上,我正在尝试使用 jQuery ajax 从视图中调用控制器,但它没有调用控制器。 我要做的就是将我的令牌值从注册页面传递给控制器​​,以便我将其值用于用户注册。

< script type = "text/javascript" >

document.getElementById('LoginWithAmazon').onclick = function() {
    options = {
        scope: 'profile'
    };
    amazon.Login.authorize(options,
    function(response) {
        if (response.error) {
            alert('oauth error ' + response.error);
            return;
        }
        GetProfileInfo(response.access_token);
    });
    function GetProfileInfo(token) {

        $.ajax({
            type: 'GET',
            url: '/Account/ProfileInfo',
            data: {
                token: 'abc'
            },
            cache: false,
            success: function(result) {
                alert(result);
            }
        });

    }

    function receiveResponse(response) {
        if (response != null) {
            for (var i = 0; i < response.length; i++) {
                alert(response[i].Data);
            }
        }
    }
    return false;
};

< /script>/

下面是我的控制器代码

public JsonResult ProfileInfo(string token) {
    return Json("test", JsonRequestBehavior.AllowGet);
}

我需要将令牌值从注册页面传递给我的控制器

【问题讨论】:

  • 检查控制台是否有错误
  • 而且必须是data:JSON.stringify({ token: 'abc'}),
  • 请检查控制台是否有错误。对于 URL ,使用 url: 'Url.Action("ProfileInfo", "Account")'
  • 没有控制台错误。我已经按照您的说法对数据和 url 进行了尊重的更改,现在它在控制台中为 url 提供错误(未找到 404):“localhost:22017/Account/…?{%22token%22 :%22abc%22}&_=1564121140550"
  • 添加 url:'@Url.Action("actionName", "controllerName")' 然后试试

标签: c# jquery ajax asp.net-mvc model-view-controller


【解决方案1】:

尝试在控制器中更改它

return Json("test", JsonRequestBehavior.AllowGet);

进入

enter code herereturn Json(new { value="test" }, JsonRequestBehavior.AllowGet);

把你的js改成这样

 $.ajax({
            type: 'GET',
            url: '/Account/ProfileInfo',
            data: JSON.stringify({
                token: 'abc'
            }),
            cache: false,
            success: function(result) {
                alert(result);
            }
        });

【讨论】:

  • 仍在寻找解决方案。其他人有解决方案吗??
【解决方案2】:

最后,我解决了这个问题。我无法调用帐户控制器,所以我使用了我的家庭控制器。下面是我用来调用 的代码>控制器

<script type="text/javascript">
document.getElementById('LoginWithAmazon').onclick = function() {
options = { scope : 'profile' };
amazon.Login.authorize(options, function(response) {
if ( response.error ) {
alert('oauth error ' + response.error);
return;
}
GetProfileInfo(response.access_token);
});

function GetProfileInfo(token)
{

 var url = "/home/ProfileInfo?token=" + token;

        var request = $.ajax({
            url: url, 
            method: "GET",

            dataType: "json"
        });

        request.done(function (msg) {
            var data = [];
           alert(msg);

        });

        request.fail(function (jqXHR, textStatus) {

        });
}
}
</script>

【讨论】:

    猜你喜欢
    • 2015-01-15
    • 2017-03-30
    • 1970-01-01
    • 2015-07-19
    • 2011-12-21
    • 1970-01-01
    • 2015-11-26
    • 1970-01-01
    相关资源
    最近更新 更多