【发布时间】:2013-06-06 19:04:26
【问题描述】:
我正在使用 ASP MVC 和 C#,我正在尝试做的事情类似于以下内容。
public JsonResult SendMessage(SMSTestViewModel model)
{
if (//logic here)
{
string phonenumber = model.phone.ToString();
string derp = string.Empty;
//SMS.SendSMS(phonenumber, "testing heheheheh", derp);
var result = new { Success = "True", Message = "Good Job" };
return Json(result, JsonRequestBehavior.AllowGet);
}
var result = new { Success = "False", Message = "Didn't work" };
return Json(result, JsonRequestBehavior.AllowGet);
}
这是我的控制器中的代码块,现在我尝试在我的视图中使用以下代码引用它
<p>Please enter your phone number</p>
<table>
<tr>
<td>Phone Number</td>
<td> <input id = "phone" type ="text" name= "phone" /></td>
</tr>
</table>
<input type="button" value="Submit" id="sendMSG">
<script>
$('sendMSG').click(function(){
$.getJSON('SMSTestController/SendMessage', function (data) {
alert(data.Success);
alert(data.Message);
});
});
</script>
由于某种原因,警报不会显示。这让我很困惑。 我对 JSON、JQuery 和 Javascript 非常陌生,因此我们将不胜感激任何帮助或建议。
谢谢
编辑:
将 html 代码块更改为以下内容:
<input type="button" value="Submit" id="sendMSG">
<script>
$('#sendMSG').click(function () {
$.getJSON('@Url.Action("SendMessage","SMSTestController")', function (data) {
alert(data.Success);
alert("test");
alert(data.Message);
});
});
</script>
【问题讨论】:
-
你遇到了什么问题?
-
@Matt Houser 警报没有出现 :( 抱歉,我什至不知道我为什么不写那个。
-
您的点击事件是否触发?你的动作会被调用吗?这里有很多失败点。在管道的哪个阶段出现问题?
-
@user2094139:JavaScript 控制台中是否有错误? JavaScript 代码会被执行吗?如果是,它是否成功到达控制器操作?
-
所以我相信我遇到的问题是它到达控制器操作,因为我在 JSonResult SendMessage 中放置了几个断点,在单击提交按钮后我从来没有打过这些断点。 ——
标签: javascript jquery json asp.net-mvc-4 asp.net-ajax