ASP.NET MVC 4: cannot modify jQuery Unobtrusive Ajax

微笑、不失礼 提交于 2019-12-03 17:11:46

问题


Using ASP.NET MVC 4 and NuGet to manage packages.

After upgrading to jQuery 1.9.1 via NuGet, I began getting JavaScript errors regarding the removal of the live() function in jQuery 1.9.x.

I hit F5 to run in debugging mode from VS.NET, go to the login page, and get the following:

Found this StackOverflow answer: https://stackoverflow.com/a/14512797 and made the 4 changes to ~\Scripts\jquery.unobtrusive-ajax.js.

My ~\Scripts\jquery.unobtrusive-ajax.js:

    $(document).on("click", "a[data-ajax=true]", function (evt) {
       ...
    });
    $(document).on("click", "form[data-ajax=true] input[type=image]", function (evt) {
       ...
    });
    $(document).on("click", "form[data-ajax=true] :submit", function (evt) {
       ...
    });
    $(document).on("submit", "form[data-ajax=true]", function (evt) {
       ...
    });

I also physically deleted jquery.unobtrusive-ajax.min.js and used WebGrease 1.3.0 to regenerate it from my updated ~\Scripts\jquery.unobtrusive-ajax.js.

However, for some reason, my changes to not use the .live() function are not sticking. I tried stopping the IIS Express 8.0 Web Site (localhost:63798) under which VS.NET is running the MVC App.

Also tried doing a Build->Clean Solution, Build->Rebuild Solution before hitting F5 to run in debug mode again.

If anyone has experienced this before and has any insight, I would be very grateful. Thank you in advance.


回答1:


The Microsoft jQuery Unobtrusive Ajax package has been fixed in version 2.0.30116.0 to fix this problem. The calls to .live() were changed to use .on().

Try upgrading your NuGet package to get the latest version.

However, the suggestion to use the jQuery Migrate package is a good one to find issues in other plugins or your own code that aren't compatible with the changes in jQuery 1.9+.




回答2:


live() has been removed in jQuery 1.9 along with a few other features.

If you need to have support for removed features you need to also add the jQuery migrate file which contains the removed features.

Adding the reference similar to below should include all the features you need:

<script src="http://code.jquery.com/jquery-migrate-1.1.1.js"></script>

The migrate file in debug mode will also add warnings to your console notifying you when you use any removed features. This will help you in slowly replacing them as you go along leading to eventually you being able to remove the migrate file reference, using only jQuery 1.9 and beyond.

See the following jQuery blog posts for all the details on migrating to jQuery 1.9.

  • jQuery Core 1.9 Upgrade Guide
  • jQuery 1.9 and 2.0 — TL;DR Edition
  • jQuery 1.9.1 Released
  • jQuery Migrate 1.1.1 Released
  • Migrate on GitHub: Migrate older jQuery code to jQuery 1.9+


来源:https://stackoverflow.com/questions/15372108/asp-net-mvc-4-cannot-modify-jquery-unobtrusive-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!