C# 简单的getJSON不起作用
JSON函数(索引)不会激发。有什么想法吗C# 简单的getJSON不起作用,c#,javascript,jquery,asp.net-mvc,C#,Javascript,Jquery,Asp.net Mvc,JSON函数(索引)不会激发。有什么想法吗 <script type="text/javascript"> $(document).ready(function() { alert("This alert is displayed :("); $("form[action$='GetQuote']").submit(function() { $.getJSON($(this).attr("
<script type="text/javascript">
$(document).ready(function() {
alert("This alert is displayed :(");
$("form[action$='GetQuote']").submit(function() {
$.getJSON($(this).attr("action"), $(this).serialize(), function(Result) {
alert("This alert is not shown :(");
$("#name").html(Result.name);
$("#address").html(Result.address);
});
return false;
});
});
</script>
首先,您可能应该使用
$.ajax
函数,并指定一个“POST”来发布您的数据名
其次,您可能需要防止发生默认事件:
$("form[action$='GetQuote']").submit(function(event) {
if (event.preventDefault) // Older I.E. uses an old DOM model, which dosen't have this event
event.preventDefault();
$.ajax(...) // Do your ajax call
return false; // Once again, for I.E.
});
submit事件可能正在覆盖与jQuery绑定的onSubmit。如果在默认情况下尝试使用HTTP GET执行此操作,ASP.NET MVC 2.0将抛出错误。您可以将其设置为帖子,也可以按照本文的建议添加说明: 即:
返回Json(数据,JsonRequestBehavior.AllowGet) 方法
isNullOrEmpty
是返回字符串还是返回布尔值?它返回字符串,但我做了一些更改以适应null或empty。请参阅EditUse:if(String.IsNullOrEmpty(dataName))
这是一种公认的做法。
$("form[action$='GetQuote']").submit(function(event) {
if (event.preventDefault) // Older I.E. uses an old DOM model, which dosen't have this event
event.preventDefault();
$.ajax(...) // Do your ajax call
return false; // Once again, for I.E.
});