我想在视图端 javascript 中使用控制器中的数据

这是我的控制器:


public ActionResult AjaxSmsSend(Sms smsInfo)

        {

            var sms = smsInfo.smsCode;

            var telephone = smsInfo.telephone;

            ViewBag.Code = sms;  

            return Json(sms); 

        }

在视图中:


<button id="getDataBtn">Click me</button>

当我按下这个按钮时:


<script type="text/javascript">

    $(function () {

        $("#getDataBtn").click(function () {

            $.ajax({

                type: "GET",

                url: "/Home/AjaxSmsSend",

                data: sms,

                contentType: "application/json; charset=utf-8",

                dataType: "json", 

        });

    });

</script>

我将用它来比较从控制器收到的短信数据。


<p style="text-align: center;margin-top: 10%;">Enter your sms code</p>

<input type="text" id="pincode" maxlength="4">

此输入由用户输入。我已在控制器本身中生成短信数据。

我确信这实际上是一个非常简单的过程。但由于我刚刚开始,我找不到我要找的东西。如果您能帮助我,我将不胜感激。


肥皂起泡泡
浏览 99回答 1
1回答

互换的青春

我认为这不是一个好的做法,通常比较应该在服务器端进行,但您可以添加以下内容:&nbsp;success: function (response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (response.smsCode)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }它会是这样的:<script type="text/javascript">&nbsp; &nbsp; $(function () {&nbsp; &nbsp; &nbsp; &nbsp; $("#getDataBtn").click(function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: "GET",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: "/Home/AjaxSmsSend",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: sms,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contentType: "application/json; charset=utf-8",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType: "json",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function (response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (response.smsCode)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });</script>然后将您的代码放入其中 if :if (response.smsCode)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }仅此而已,希望对您有所帮助
打开App,查看更多内容
随时随地看视频慕课网APP