Jquery ajax 请求显示 json 对象而不是警报

Jquery ajax request showing json object instead of alerting

提交表单后,显示 Json 对象而不是警报。我需要的是一个显示成功消息或错误的警告框。但是在提交表单时,它只显示返回的 JSON 对象。这是我的 MVC 控制器操作:

[HttpPost]
    public ActionResult Subscribe(Subscriber Sub)
    {
        KeyGenerator Validate = new KeyGenerator();
        string Email = Sub.Username.Trim();
        bool IsValid = Validate.ValidateEmail(Email);
        ENT Ent = new ENT();
        if (IsValid)
        {
            Subscriber SubEmail = Ent.Subscribers.Where(a => a.Username == Email).FirstOrDefault();
            if (SubEmail != null)
            {
                return Json(new { success = true, responseText = "Already subscribed!" }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                Ent.AddToSub(Email);
                return Json(new { success = true, responseText = "Thank you." }, JsonRequestBehavior.AllowGet);
            }
        }
        else
        {
            return Json(new { success = false, responseText = "Invalid request." }, JsonRequestBehavior.AllowGet);
        }
    }

这是我的 ajax 请求:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
        $("#subscribe").submit(function(e) {
        e.preventDefault();
        var values = new FormData($(this));
        $.ajax({
               type: $(this).type,
               url: $(this).action,
               data: values,
               processData: false,
               contentType: false,
               cache: false,
               success: function (response) {
                if (response.success) {
                    alert(response.responseText);
                }
                else {
                    alert(response.responseText);
                }                          
                },
                error: function (response) {
                    alert(response.responseText);
                }
             });
        
    });

如何显示警报而不是 JSON 对象? 我的 JSON 回复如下:

{"success":true,"responseText":"Thank you."}

另外,这是因为请求的页面是PHP吗? 请求的页面在https://MyWebUrl.com(不是真实的url) 在 PHP MVC 网站位于 https://MySubDomain。MyWebUrl.com(不真实 url) 是这个原因吗?

这可能不是解决方案,但对我有用。由于我问题中粗体字所列的原因,我做了一些修改。 我将 MVC 操作更改为:

    [HttpPost]
    public ActionResult Subscribe(Subscriber Sub)
    {
        KeyGenerator Validate = new KeyGenerator();
        string Email = Sub.Username.Trim();
        bool IsValid = Validate.ValidateEmail(Email);
        ENT Ent = new ENT();
        if (IsValid)
        {
            Subscriber SubEmail = Ent.Subscribers.Where(a => a.Username.ToLower() == Email.ToLower()).FirstOrDefault();
            if (SubEmail != null)
            {
                //return Json(new { success = true, responseText = "Already subscribed!" }, JsonRequestBehavior.AllowGet);
                return RedirectToAction("ViewSubMsg", new { id = "Already subscribed!" });
            }
            else
            {
                Ent.AddToSub(Email);
                //return Json(new { success = true, responseText = "Thank you." }, JsonRequestBehavior.AllowGet);
                return RedirectToAction("ViewSubMsg", new { id = "Thank you!" });
            }
        }
        else
        {
            //return Json(new { success = false, responseText = "Invalid request!" }, JsonRequestBehavior.AllowGet);
            return RedirectToAction("ViewSubMsg", new { id = "Invalid request!" });
        }
    }

然后我添加了一个新动作:

    [HttpGet]
    public ActionResult ViewSubMsg(string id)
    {
        if (string.IsNullOrEmpty(id))
        {
            return Redirect("mysiteurl");
        }
        else
        {
            ViewBag.Msg = id;
            return View();
        }
    }

及其观点:

//View    
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>ViewSubMsg</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
    <script>
        $(document).ready(function () {
            alert("@ViewBag.Msg");
            window.location.replace("mysiteurl");
        });
    </script>
</body>
</html>