【发布时间】:2012-06-24 17:12:59
【问题描述】:
我正在尝试使用 getJSON() 调用使 ajax 工作。 当我导航到 /Home/GetWeather 时,我会在浏览器中返回 Json 数据。 但是 jQuery 调用不起作用。有任何想法吗? 当我在警报(“Hello”)上设置断点时,它从未命中。 在萤火虫中,我没有看到任何 ajax 调用。有什么想法吗?
$(document).ready(function() {
$("#Button1").click(function() {
$.getJSON("/Home/GetWeather", null, function(data) {
alert("Hello");
});
});
});
控制器代码
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
public JsonResult GetWeather()
{
List<Weather> weather = new List<Weather>();
// populate weather with data
return Json(weather, JsonRequestBehavior.AllowGet);
}
}
查看:
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
<script src="../../Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-1.5.1-vsdoc.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#Button1").click(function () {
$.getJSON("/Home/GetWeather", null, function (data) { //I hit breakpoint here, but in firebug I dont see any call, nothing
alert("Hello"); // Breakpoint here doesnt hit :(
});
});
});
</script>
</head>
<body>
<input id="Button1" type="button" name="Button1" value="Get Weather" />
<div id="Weather"></div>
</body>
</html>
我找到的解决方案被删除的任何原因???
【问题讨论】:
-
控制器的正确路径?可能类似于
../Home/GetWeather。此外,您可以从通话中删除null -
检查您对同源策略没有任何问题。见:stackoverflow.com/questions/2538215/…
-
@CharlieKilian。他正在向控制器发送数据,他怎么会违反同源策略?
-
您是否在 firebug 或 chrome 开发工具中遇到任何错误?你是否包含了 jquery.js?是
#Button1锚标签吗? -
现在您更新您的问题...?第一次你真的应该写一个答案,你编辑你的问题......请这样做。
标签: jquery asp.net-mvc asp.net-mvc-3 getjson