简体   繁体   中英

Message from stored procedure to alert box in view

I am sending parameters to a stored procedure and the stored procedure return a message back, it's either a success or an error.

public IActionResult Index() => View();

public async Task<IActionResult> Upload(IFormFile file) 
{
    int cmdResponse = Convert.ToInt32(cmd.Parameters["@response"].Value);
            string cmdMessage = cmd.Parameters["@message"].Value.ToString();
            if (cmdResponse == 0)
            {
                con.Close();
                return RedirectToAction("Index", Json(new { status = "error", message = cmdMessage }));
            }
            return RedirectToAction("Index", Json(new { status = "success", message = cmdMessage }));

}

and here is the view with the Javascript where I try to display the message from the stored procedure in an alert box.

<script>

    $(function () {
        $("#btn").click(function () {
            dangerResponseMessage('btn');
        });
    });

    function dangerResponseMessage(result) {
        var url = window.rootUrl + 'Upload/Upload';

        $.ajax({
            type: "GET",
            url: url,
            dataType: 'json',
            success: function (result) {
                alert(result.message);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(result.message);
            }
        });
    }
</script>

The problem is that the alert box displays undefined在此处输入图像描述

and when I dismiss the alert, it return the message from the stored procedure in the URL in the browser. 在此处输入图像描述

The cyrillic sentence in the URL is the message I am trying to display in the alert box. cmdMessage 的正确值 The ajax call is without data, since I don't need to send anything from the view to the controller. Any ideas as why is it showing the message in the URL and not in the alert box?

Have you tried just console.log(result) to just confirm exactly what you're getting back? If you're code in failing in 'error', then does 'result exist? Looks like it belongs to 'success' which may also be why it's failing and showing up on the address bar.

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