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

我正在尝试从 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");

            }

        }


饮歌长啸
浏览 224回答 2
2回答

海绵宝宝撒

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

达令说

您应该将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: ');    }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript