简体   繁体   English

在 ajax function 中成功后打开 url 在 Z1848E732214CCE0460F9B24F0C 内核中

[英]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.我正在尝试从 Ajax function 中打开 URL,但 ZE6B391A23D2C4D485703A

I am using the callback because I want my data to be completed in another domain (using my javascript).我正在使用回调,因为我希望我的数据在另一个域中完成(使用我的 javascript)。

This in turn I recondition it with the iactionresult method in Asp.Net Core这反过来我用 Asp.Net Core 中的 iactionresult 方法对其进行了修复

What I want is that when the affirmative answer arrives I open the url that I point in my ajax method我想要的是,当肯定的答案到达时,我打开我在 ajax 方法中指向的 url

I am working with iss express and vs 2019我正在使用 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");
            }
        }

You have annotate with [FromBody] or use primitive types for controller method parameters.您已使用[FromBody]进行注释或对 controller 方法参数使用原始类型。

[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]

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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