【问题标题】:Trying to redirect from AJAX post尝试从 AJAX 帖子重定向
【发布时间】:2017-09-20 10:42:35
【问题描述】:

我正在尝试从 AJAX 调用 Web 方法,该方法重定向到这样的 url:

HttpContext.Current.Response.Redirect(StoreCode.ToLower() + "/App/home.html?EventClick=True", false);

AJAX 调用:

function TokenPost() {
    var token = $('#hdnToken').val();
    $.ajax({
        url: 'http://localhost/QDC/WebServices.asmx/ChkToken',
        type: 'POST',
        data: "{data:'" + token + "'}",
        contentType: 'application/json; charset=UTF-8',
        datatype: 'JSON',
        success: function(response) {

        },
        error: function(response) {
            alert(response.d);
        }
    });
}

但我的 AJAX 调用没有重定向到该 url。提前致谢。

【问题讨论】:

  • 您使用 Asp.net 网络表单、mvc 还是 webapi ?您的请求是否到达后端?
  • asp.net webforms 和我的请求命中后端,但没有重定向到 url。 @OlegI
  • @RachitShah 你收到任何消息了吗? 404等

标签: c# asp.net ajax http redirect


【解决方案1】:

我认为这在 HttpPost 方法中是不可能的。但是您可以从服务器返回 HttpPost 方法中的链接。如果 ajax 返回成功,则使用 Javascript 代码进行重定向。

        $.ajax({
            url: 'http://localhost/QDC/WebServices.asmx/ChkToken',
            type: 'POST',
            data: "{data:'" + token + "'}",
            contentType: 'application/json; charset=UTF-8',
            datatype: 'JSON',
            success: function (link) {
                window.location.href = link;
            },
            error: function (link) {
                alert(response.d);
            }
        });

在服务器方法中(我在 ASP.NET MVC 中使用它):

string link = "https://...Your Link";

return Json(link, JsonRequestBehavior.AllowGet);

如果你使用 WebMethod,我想你也可以只返回字符串。

[System.Web.Services.WebMethod]
public static string ChkToken(string data)
{
    string link = "...";
    // Do things
    return link;
}

这次ajax返回可以这样,

            success: function (link) {
                window.location.href = link.d;
            },

【讨论】:

  • 我认为作者想使用 asp.net 方法重定向,而不是将 url 作为字符串返回并通过 js 执行此操作
猜你喜欢
  • 1970-01-01
  • 2010-10-24
  • 1970-01-01
  • 1970-01-01
  • 2017-10-05
  • 1970-01-01
  • 1970-01-01
  • 2017-05-27
  • 1970-01-01
相关资源
最近更新 更多