简体   繁体   中英

Open url after success in ajax function in asp.net core

I am trying to open a URL from an Ajax function, but the URL is not called.

I am using the callback because I want my data to be completed in another domain (using my javascript).

This in turn I recondition it with the iactionresult method in Asp.Net Core

What I want is that when the affirmative answer arrives I open the url that I point in my ajax method

I am working with iss express and 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");
            }
        }

You have annotate with [FromBody] or use primitive types for controller method parameters.

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

Example as [HttpGet] .

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

You should change the ajax success: function parameters from function (data, status) to function (data) and the return Json result will be stored in data

Your code should be like:

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: ');
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM