【问题标题】:Not able Invoke controller action method from Ajax Jquery无法从 Ajax Jquery 调用控制器操作方法
【发布时间】:2020-10-13 09:25:52
【问题描述】:

我试图从 ajax 调用 mvc 核心操作方法,但在控制台中出现错误:

以下是代码: `

$.ajax({
        url: LogOutUrl,
        type: "POST",
        dataType: "html",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            if (data.status == "Success") {
                alert("Done");
                if (window.location.href.indexOf(EserviceLogOutUrl) === -1) {
                    window.location.href = EserviceLogOutUrl;
                }
            } else {
                alert("Error occurs on the Database level!");
            }
        },
        error: function () {
            alert("An error has occured!!!");
        }
    });

MVC:

MVC:控制器:

public ActionResult Signout()
        {
            foreach (var cookie in Request.Cookies.Keys)
            {
                Response.Cookies.Delete(cookie);
            }
            _httpContextAccessor.HttpContext.Session.Clear();
            return new RedirectResult("http://Google.com");//Redirecting to different website in antoher domain.
        }

控制台错误:我能够调用操作方法。但无法重定向 URL。

SEC7123:请求标头 x-requested-with 不在 Access-Control-Allow-Headers 列表中。

【问题讨论】:

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


    【解决方案1】:

    在选项中添加标题以满足您的服务器:

    
    $.ajax({
            url: LogOutUrl,
            type: "POST",
            dataType: "html",
            contentType: "application/json; charset=utf-8",
            headers: {'X-Requested-With': 'XMLHttpRequest'},
            ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 2021-10-09
      • 1970-01-01
      • 2021-06-16
      相关资源
      最近更新 更多