将 jQuery 更新到 1.9.0 后,不显眼的 Ajax 停止工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14391867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unobtrusive Ajax stopped working after update jQuery to 1.9.0
提问by monstro
I have just updated jQuery & jQuery UI to: jquery-1.9.0.min.js and jquery-ui-1.9.2.min.js
我刚刚将 jQuery 和 jQuery UI 更新为:jquery-1.9.0.min.js 和 jquery-ui-1.9.2.min.js
And... all my unobtrusive Ajax calls (Ajax.ActionLink, Ajax.BeginForm) stopped working properly - they open results in a new page instead of updating the existing div.
而且...我所有不显眼的 Ajax 调用(Ajax.ActionLink、Ajax.BeginForm)都停止正常工作——它们在新页面中打开结果,而不是更新现有的 div。
And I get this javascript error in Firebug when my page loads:
当我的页面加载时,我在 Firebug 中收到这个 javascript 错误:
Code hasn't changed of course, just updated the jQuery scripts using Nuget.
代码当然没有改变,只是使用 Nuget 更新了 jQuery 脚本。
Anyone experienced the same problem ??
有人遇到过同样的问题吗??
采纳答案by Oliver Spryn
.live()
has been deprecated since 1.7 and was officially removed in jQuery 1.9. Use .on()
instead as it is the preferred method of doing the same thing.
.live()
自 1.7 以来已被弃用,并在 jQuery 1.9 中被正式删除。改用.on()
它,因为它是做同样事情的首选方法。
回答by jason_ruz
Update:This issue has been fixed in the latest NuGet package. I've posted another answer to reflect this. https://stackoverflow.com/a/15539422/714309
更新:此问题已在最新的 NuGet 包中修复。我已经发布了另一个答案来反映这一点。 https://stackoverflow.com/a/15539422/714309
In jquery.unobtrusive-ajax.js
, find and replace these four lines:
在 中jquery.unobtrusive-ajax.js
,找到并替换这四行:
$("a[data-ajax=true]").live("click", function (evt) {
$(document).on("click", "a[data-ajax=true]", function (evt) {
$("form[data-ajax=true] input[type=image]").live("click", function (evt) {
$(document).on("click", "form[data-ajax=true] input[type=image]", function (evt) {
$("form[data-ajax=true] :submit").live("click", function (evt) {
$(document).on("click", "form[data-ajax=true] :submit", function (evt) {
$("form[data-ajax=true]").live("submit", function (evt) {
$(document).on("submit", "form[data-ajax=true]", function (evt) {
$("a[data-ajax=true]").live("click", function (evt) {
$(document).on("click", "a[data-ajax=true]", function (evt) {
$("form[data-ajax=true] input[type=image]").live("click", function (evt) {
$(document).on("click", "form[data-ajax=true] input[type=image]", function (evt) {
$("form[data-ajax=true] :submit").live("click", function (evt) {
$(document).on("click", "form[data-ajax=true] :submit", function (evt) {
$("form[data-ajax=true]").live("submit", function (evt) {
$(document).on("submit", "form[data-ajax=true]", function (evt) {
You can also generate a new jquery.unobtrusive-ajax.min.js
using WebGrease.
From the Command Prompt, change to your solution folder and enter this command (assuming your project folder is called Web
):
您还可以jquery.unobtrusive-ajax.min.js
使用 WebGrease生成新的。在命令提示符下,切换到您的解决方案文件夹并输入以下命令(假设您的项目文件夹名为Web
):
packages\WebGrease.1.3.0\tools\WG.exe -m -in:Web\Scripts\jquery.unobtrusive-ajax.js -out:Web\Scripts\jquery.unobtrusive-ajax.min.js
回答by jason_ruz
Update the Microsoft jQuery Unobtrusive AjaxNuGet package to the latest version.
将Microsoft jQuery Unobtrusive AjaxNuGet 包更新到最新版本。
In Visual Studio, from the Toolsmenu, select Library Package Managerand then click Package Manager Console. At the prompt, type:
在 Visual Studio 中,从“工具”菜单中选择“库包管理器”,然后单击“包管理器控制台”。在提示符下,键入:
Update-Package Microsoft.jQuery.Unobtrusive.Ajax
This issue was fixed in Microsoft jQuery Unobtrusive Ajax 2.0.30116.0 (Monday, February 18, 2013), which replaced the calls to the .live()
method (deprecated in jQuery 1.7 and removed from jQuery 1.9) with the recommended .on()
method.
此问题已在 Microsoft jQuery Unobtrusive Ajax 2.0.30116.0(2013 年 2 月 18 日,星期一)中得到修复,该版本将对该.live()
方法的调用(在 jQuery 1.7 中弃用并从 jQuery 1.9 中删除)替换为推荐的.on()
方法。
回答by Caleb Kiage
The easiest way to fix problems with the breaking changes in jQuery(in my opinion) is to install the jQuery.Migrate
package that enables you to use deprecated function calls that were removed in version 1.9.0 onwards. At least until the unobtrusive ajax plugin is updated by Microsoft.
解决 jQuery 中的重大更改问题的最简单方法(在我看来)是安装jQuery.Migrate
包,该包使您能够使用在 1.9.0 版本中删除的已弃用的函数调用。至少在 Microsoft 更新不显眼的 ajax 插件之前。
Also, it appears that the nightly build of the unobtrusive ajax plugin have updated the api calls. I haven't tested it yet but you may find out how to acquire it in the asp.net codeplex page.
此外,似乎不引人注目的 ajax 插件的夜间构建更新了 api 调用。我尚未对其进行测试,但您可以在asp.net codeplex 页面中找到如何获取它。
UPDATE:
The Unobtrusive Ajax and Validation Nuget packages were updated so the jQuery.Migrate
package isn't needed anymore.
更新: Unobtrusive Ajax 和 Validation Nuget 包已更新,因此jQuery.Migrate
不再需要该包。
回答by Simon_Weaver
Also need to do these fixes :
还需要做这些修复:
Update jQuery.Validation plugin (to fix this problem)
更新 jQuery.Validation 插件(解决这个问题)
https://nuget.org/packages/jQuery.Validation/1.11.0 (first available on nuget 2/4/13)
Also make these changes to the jquery.unobtrusive-ajax.js
file (see here for Connect issue)
还要对jquery.unobtrusive-ajax.js
文件进行这些更改(有关连接问题,请参见此处)
Line 43: replace = container.attr("data-valmsg-replace") && $.parseJSON(container.attr("data-valmsg-replace")) !== false;
Line 73: replace = container.attr("data-valmsg-replace") && $.parseJSON(container.attr("data-valmsg-replace"));
回答by Nabil Kadimi
Replace
代替
.live(function)
With
和
.on(eventType, selector, function)
回答by Christian F Patzer
Here is an actual minified file that should work for you. Works for me anyways. I just manually edited it.
这是一个实际的缩小文件,应该适合您。反正对我有用。我只是手动编辑它。
/*
** Unobtrusive Ajax support library for jQuery
** Copyright (C) Microsoft Corporation. All rights reserved.
*/
(function(a){var b="unobtrusiveAjaxClick",g="unobtrusiveValidation";function c(d,b){var a=window,c=(d||"").split(".");while(a&&c.length)a=a[c.shift()];if(typeof a==="function")return a;b.push(d);return Function.constructor.apply(null,b)}function d(a){return a==="GET"||a==="POST"}function f(b,a){!d(a)&&b.setRequestHeader("X-HTTP-Method-Override",a)}function h(c,b,e){var d;if(e.indexOf("application/x-javascript")!==-1)return;d=(c.getAttribute("data-ajax-mode")||"").toUpperCase();a(c.getAttribute("data-ajax-update")).each(function(f,c){var e;switch(d){case"BEFORE":e=c.firstChild;a("<div />").html(b).contents().each(function(){c.insertBefore(this,e)});break;case"AFTER":a("<div />").html(b).contents().each(function(){c.appendChild(this)});break;default:a(c).html(b)}})}function e(b,e){var j,k,g,i;j=b.getAttribute("data-ajax-confirm");if(j&&!window.confirm(j))return;k=a(b.getAttribute("data-ajax-loading"));i=b.getAttribute("data-ajax-loading-duration")||0;a.extend(e,{type:b.getAttribute("data-ajax-method")||undefined,url:b.getAttribute("data-ajax-url")||undefined,beforeSend:function(d){var a;f(d,g);a=c(b.getAttribute("data-ajax-begin"),["xhr"]).apply(this,arguments);a!==false&&k.show(i);return a},complete:function(){k.hide(i);c(b.getAttribute("data-ajax-complete"),["xhr","status"]).apply(this,arguments)},success:function(a,e,d){h(b,a,d.getResponseHeader("Content-Type")||"text/html");c(b.getAttribute("data-ajax-success"),["data","status","xhr"]).apply(this,arguments)},error:c(b.getAttribute("data-ajax-failure"),["xhr","status","error"])});e.data.push({name:"X-Requested-With",value:"XMLHttpRequest"});g=e.type.toUpperCase();if(!d(g)){e.type="POST";e.data.push({name:"X-HTTP-Method-Override",value:g})}a.ajax(e)}function i(c){var b=a(c).data(g);return!b||!b.validate||b.validate()}a(document).on("click", "a[data-ajax=true]", function(a){a.preventDefault();e(this,{url:this.href,type:"GET",data:[]})});a(document).on("click", "form[data-ajax=true] input[type=image]", function(c){var g=c.target.name,d=a(c.target),f=d.parents("form")[0],e=d.offset();a(f).data(b,[{name:g+".x",value:Math.round(c.pageX-e.left)},{name:g+".y",value:Math.round(c.pageY-e.top)}]);setTimeout(function(){a(f).removeData(b)},0)});a(document).on("click","form[data-ajax=true] :submit", function(c){var e=c.target.name,d=a(c.target).parents("form")[0];a(d).data(b,e?[{name:e,value:c.target.value}]:[]);setTimeout(function(){a(d).removeData(b)},0)});a(document).on("submit","form[data-ajax=true]",function(d){var c=a(this).data(b)||[];d.preventDefault();if(!i(this))return;e(this,{url:this.action,type:this.method||"GET",data:c.concat(a(this).serializeArray())})})})(jQuery);
回答by shaijut
Just update your Scripts
只需更新您的脚本
1.Download latest Jquery, (I used jquery-1.11.0 though)
1.下载最新的Jquery,(虽然我用的是jquery-1.11.0)
2.Download latest Microsoft.jQuery.Unobtrusive.Ajax from from Microsoft official
2.从微软官方下载最新的Microsoft.jQuery.Unobtrusive.Ajax
From here:
从这里:
https://github.com/aspnet/jquery-ajax-unobtrusive
https://github.com/aspnet/jquery-ajax-unobtrusive
3.Do hard refresh or Clear your browser cache and check your page again.
3. 进行硬刷新或清除浏览器缓存并再次检查您的页面。
Hope helps someone.
希望能帮助某人。