为了账号安全,请及时绑定邮箱和手机立即绑定

在asp.net core的ajax函数中成功后打开url

在asp.net core的ajax函数中成功后打开url

饮歌长啸 2021-11-25 19:44:44
我正在尝试从 Ajax 函数打开 URL,但未调用该 URL。我使用回调是因为我希望我的数据在另一个域中完成(使用我的 javascript)。这反过来我用 Asp.Net Core 中的 iactionresult 方法对其进行了修复我想要的是,当肯定的答案到达时,我打开我在 ajax 方法中指向的 url我正在使用 iss express 和 vs 2019function 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");            }        }
查看完整描述

2 回答

?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

您已[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;

    ...

}


查看完整回答
反对 回复 2021-11-25
?
达令说

TA贡献1821条经验 获得超6个赞

您应该将ajaxsuccess:函数参数从function (data, status)to更改,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: ');

    }

}


查看完整回答
反对 回复 2021-11-25
  • 2 回答
  • 0 关注
  • 223 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信