当我没有引用不引人注目的Ajax脚本时,我有一个Ajax调用可以正常工作。当我将它添加到页面时,它会阻止onSuccess Javascript被触发。当尝试使用引用的脚本呈现部分视图时,一切正常。另外,奇怪的是onSuccess函数在页面首次加载时触发。
为了澄清,我在DOM的底部引用了脚本,并且web.config文件已启用它。当使用浏览器调试时,我看到AJAX是成功的,它只是没有进入javascript onSuccess方法。这是我的代码:
参考脚本:
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/Scripts/jquery-ui-1.10.4.min.js")
@Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js")
@Scripts.Render("~/Scripts/jquery.validate.unobtrusive.min.js")
@Scripts.Render("~/Scripts/AAData.js")
@RenderSection("scripts", required: false)
</body>
</html>
AJAX致电:
@using (Ajax.BeginForm("Search", "Home", new AjaxOptions { OnSuccess = "onSuccess", HttpMethod = "GET", UpdateTargetId = "Search" }))
{
<div class="col-md-6 form-inline">
<div class="form-group" style="width:85%;">
<div class="right-inner-addon">
<i class=" glyphicon glyphicon-search"></i>
<input type="text" data-autocomplete="@Url.Action("Quicksearch","Home")" class="form-control" placeholder="Search" name="q" value="@ViewBag.CurrenetFilter" />
</div>
</div>
<div class="form-group">
<button class="btn btn-default form-inline" type="submit">Search</button>
</div>
<br />
</div>
}
控制器:
public ActionResult Search(string q, int? page, string catb = null)
{
return Json(new { url = Url.Action("Search", "Items", new { query = q, pageNum = page, categoryB = catb }) }, JsonRequestBehavior.AllowGet);
}
使用Javascript:
var onSuccess = function (result) {
if (result.url) {
// if the server returned a JSON object containing an url
// property we redirect the browser to that url
window.location.href = result.url;
}
答案 0 :(得分:0)
您是否尝试过使用OnComplete而不是OnSuccess?
http://msdn.microsoft.com/en-us/library/system.web.mvc.ajax.ajaxoptions.oncomplete(v=vs.118).aspx
答案 1 :(得分:0)
我将脚本的顺序和更新的jquery更改为最新版本以便解决此问题。脚本按以下顺序排列:
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/Scripts/jquery.validate.min.js")
@Scripts.Render("~/Scripts/jquery.validate.unobtrusive.min.js")
@Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js")
Jquery是版本2.1.1
不显眼的ajax是3.1.2