Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Jquery ajax请求显示json对象而不是发出警报_C#_Jquery_Ajax_Forms_Model View Controller - Fatal编程技术网

C# Jquery ajax请求显示json对象而不是发出警报

C# Jquery ajax请求显示json对象而不是发出警报,c#,jquery,ajax,forms,model-view-controller,C#,Jquery,Ajax,Forms,Model View Controller,提交表单后,将显示Json对象而不是警报。我需要的是一个显示成功消息或错误的警报框。但是在提交表单时,它只显示返回的JSON对象。这是我的MVC控制器操作: [HttpPost] public ActionResult Subscribe(Subscriber Sub) { KeyGenerator Validate = new KeyGenerator(); string Email = Sub.Username.Trim();

提交表单后,将显示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);
                }
             });
        
    });
还有,这是因为请求页面是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>
//查看
@{
布局=空;
}
ViewSubMsg
$(文档).ready(函数(){
警报(“@ViewBag.Msg”);
window.location.replace(“mysiteurl”);
});

可以尝试将响应转换为json对象
让json=json.parse(响应)警报(json.ResponseText)我知道您正在从控制器返回一个json对象,但值得一试。我这样做了,但仍然不起作用。请检查我的编辑。
//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>