Javascript 未使用$.getJSOn asp.net MVC3显示警报
我的主布局中有一个actionlink,如下所示:Javascript 未使用$.getJSOn asp.net MVC3显示警报,javascript,jquery,asp.net,asp.net-mvc-3,Javascript,Jquery,Asp.net,Asp.net Mvc 3,我的主布局中有一个actionlink,如下所示: @Html.ActionLink("Order Your Free Report 1", "CheckValue", "Product", null,new { id = "checkExists" }) $(function () { $('#checkExists').click(function () { $.getJSON(this.href, function (result) { ale
@Html.ActionLink("Order Your Free Report 1", "CheckValue", "Product", null,new { id = "checkExists" })
$(function () {
$('#checkExists').click(function () {
$.getJSON(this.href, function (result) {
alert(result);
if (result) {
alert('the record exists');
}
});
return false;
});
});
$(function () {
$('#checkExists').click(function () {
var condition =new Boolean('@ViewData["checkCondition"]');
if (condition) {
alert("message");
}
return false;
});
});
我有一个这样的行动方法:
public ActionResult CheckValue() {
bool result = true;
ViewData["checkCondition"] = true;
return Json(result, JsonRequestBehavior.AllowGet);
}
功能如下:
@Html.ActionLink("Order Your Free Report 1", "CheckValue", "Product", null,new { id = "checkExists" })
$(function () {
$('#checkExists').click(function () {
$.getJSON(this.href, function (result) {
alert(result);
if (result) {
alert('the record exists');
}
});
return false;
});
});
$(function () {
$('#checkExists').click(function () {
var condition =new Boolean('@ViewData["checkCondition"]');
if (condition) {
alert("message");
}
return false;
});
});
单击链接时,不会显示警报。但如果我这样使用:
@Html.ActionLink("Order Your Free Report 1", "CheckValue", "Product", null,new { id = "checkExists" })
$(function () {
$('#checkExists').click(function () {
$.getJSON(this.href, function (result) {
alert(result);
if (result) {
alert('the record exists');
}
});
return false;
});
});
$(function () {
$('#checkExists').click(function () {
var condition =new Boolean('@ViewData["checkCondition"]');
if (condition) {
alert("message");
}
return false;
});
});
它起作用了。请说明为什么第一个不起作用?尝试将
此
包装到$(此)
ajax版本
$(function () {
$('#checkExists').click(function () {
$.ajax({
url: $(this).attr('href'),
type:'GET',
success: function (result) { ... },
dataType: 'json',
error:function(jqXhr,textStatus, errorThrown){
alert("Oops ");
alert(jqXhr.responseText);
alert(jqXhr.status);
}
});
return false;
});
});
尝试此操作并告诉显示哪些警报您是否使用了Firebug或其他浏览器调试器来查看是否发出了HTTP请求?任何地方是否报告了任何错误?使用firebug检查链接的
href
属性值是多少?@3nigma:link在firebug html窗口中看起来是这样的:@Pointy:我刚刚注意到请求是在jquery$.getJson的情况下发出的。但不是在javascript警报的情况下;如果只执行第二个操作,则没有ajax调用,因此没有HTTP事务。HTTP请求是正常的还是有错误?结果是否显示为正确的JSON编码?