【问题标题】:Open url after success in ajax function in asp.net core在asp.net核心的ajax函数中成功后打开url
【发布时间】:2019-10-19 03:38:07
【问题描述】:

我正在尝试从 Ajax 函数打开一个 URL,但该 URL 未被调用。

我使用回调是因为我希望我的数据在另一个域中完成(使用我的 javascript)。

这反过来又用 Asp.Net Core 中的 iactionresult 方法对其进行了修复

我想要的是,当肯定的答案到达时,我打开我在 ajax 方法中指向的 url

我正在使用 iss express 和 vs 2019

function ValidarExisteContactoPago() {

    var Nombre;
    var IdUsuario;

    if ($("#Nombre").val() !== null || $("#IdUsuario").val() !== null) {
        IdUsuario = $("#IdUsuario").val();
        Nombre = $("#Nombre").val();

        $.ajax({
            type: "GET",
            url: "/Pago/IndexTCS",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: {
                IdUsuario: $("#IdUsuario").val(),
                Nombre: $("#Nombre").val()   
            },
            async: false,
            success: function (data, status) {
                alert("Data: " + data + "\nStatus: " + status);

                if (status === "success") {

                    window.location.href = "/Pago/IndexTC";

                } else {

                    console.log('Data received: ');

                }

            },
            error: function (data, status) {

                console.log('Data received: ');

            }
        });

    } else {

        console.log('Data received: ');

    }


}


public IActionResult IndexTC()
        {
            return View();
        }


        [HttpGet]
        public JsonResult IndexTCS(UsuarioViewModel usuarioView)
        {


            //RedirectToAction("IndexTC", "Pago");
            bool successToStoreData = SomeMethod(usuarioView);
            if (successToStoreData)
            {
                return Json(usuarioView);  // indicates success
            }
            else
            {
                return Json("Your error message");
            }
        }

【问题讨论】:

  • 我做了一个demo来测试你的代码,但是它在ajax的成功函数中打开了url。如果回调是成功函数,你是否按F12并在ajax处下断点调试?你能分享一个可以重现你的问题的演示吗?

标签: javascript c# jquery asp.net-core


【解决方案1】:

您已使用[FromBody] 进行注释或使用原始类型作为控制器方法参数。

[HttpPost]
public JsonResult IndexTCS([FromBody] UsuarioViewModel usuarioView)
{
    bool successToStoreData = SomeMethod(usuarioView);
    if (successToStoreData)
    {
        return Ok(usuarioView);  // indicates success
    }
    else
    {
        return BadRequest("Your error message");
    }
}

例如[HttpGet]。

[HttpGet]
public JsonResult IndexTCS(string someVal, string someOtherVal)
{
    ...
    UsuarioViewModel model = new UsuarioViewModel();
    model.someVal = someVal;
    model.someOtherVal = someOtherVal;
    ...
}

【讨论】:

    【解决方案2】:

    您应该将ajax success:函数参数从function (data, status)更改为function (data),返回的Json结果将存储在data中

    你的代码应该是这样的:

    function ValidarExisteContactoPago() {
    
        var Nombre;
        var IdUsuario;
    
        if ($("#Nombre").val() !== null || $("#IdUsuario").val() !== null) {
            IdUsuario = $("#IdUsuario").val();
            Nombre = $("#Nombre").val();
    
            $.ajax({
                type: "GET",
                url: "/Pago/IndexTCS",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: {
                    IdUsuario: $("#IdUsuario").val(),
                    Nombre: $("#Nombre").val()   
                },
                async: false,
                success: function (data) {
                   // alert("Data: " + data + "\nStatus: " + status);
    
                    if (data === "success") {
                        window.location.href = "/Pago/IndexTC";
    
                    } else {
                        console.log('Data received: ');
                    }
                },
                error: function (data) {
                    console.log('Data received: ');
                }
            });
    
        } else {
            console.log('Data received: ');
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-29
      • 2017-04-17
      • 1970-01-01
      • 2019-03-18
      • 1970-01-01
      • 2021-06-17
      • 2019-09-22
      • 2015-01-15
      相关资源
      最近更新 更多