简体   繁体   English

Ajax请求与普通MVC

[英]Ajax requests vs normal mvc

I am pretty new to MVC, but my research has pointed out that I can either use the default way to send requests to my controller, via forms, or I can use jQuery and Ajax requests. 我对MVC还是很陌生,但是我的研究指出,我可以使用默认方式通过表单将请求发送到控制器,也可以使用jQuery和Ajax请求。

Is it common practise to use Ajax all the time, so that I can avoid page reloads when I don't want then, and allow for loading icons? 经常使用Ajax是常见的做法,这样我就可以避免在不需要时避免页面重新加载,并允许加载图标?

Should the two ways of sending data to the controller be mixed? 将数据发送到控制器的两种方式是否应该混合使用? Using forms is easier, I guess, as you would need to coffee all your model properties manually in Ajax, I think, but it looks like you Haddin more flexibility. 我想,使用表单更容易,因为我认为您需要在Ajax中手动处理所有模型属性,但是看起来您的Haddin更具灵活性。

I'm wondering if this pattern (using predominantly Ajax requests) is a good idea. 我想知道这种模式(主要使用Ajax请求)是否是一个好主意。

I think mostly its a matter of preference but I think using AJAX with jQuery provides for a more flexible and responsive application. 我认为大多数情况下都是首选问题,但我认为将AJAX与jQuery结合使用可提供一种更加灵活和响应迅速的应用程序。 Also it doesn't have to be more work. 另外,它不必做更多的工作。 To submit all of your model properties that you had in the form to the controller you would just need to do something like: 要将表单中拥有的所有模型属性提交给控制器,您只需执行以下操作:

$.ajax({
   url: "SomeController/SomeMethod", 
   data: $('#FormName').serialize(),  //this will basically do what MVC would have done if you used forms and a submit button
});

And on the controller the model binder will bind all of your properties back up to the model. 在控制器上,模型绑定器会将您的所有属性绑定回模型。

So you would have: 因此,您将拥有:

public ActionResult SomeMethod(SomeModel someModel)
{
  // do work with your tightly bound model!
  return View();
}

But...if you just want to know the Pros of AJAX and some of the CONS that you will also encounter then here you are: 但是...如果您只是想了解AJAX的优点以及您还将遇到的一些CONS,那么您可以:

PRO: PRO:

In many cases, related pages on a website consist of much content that is common between them. 在许多情况下,网站上的相关页面包含许多彼此之间共有的内容。 Using traditional methods, that content would have to be reloaded on every request. 使用传统方法,必须在每次请求时重新加载该内容。 However, using Ajax, a web application can request only the content that needs to be updated, thus drastically reducing bandwidth usage and load time. 但是,使用Ajax,Web应用程序只能请求需要更新的内容,从而大大减少了带宽使用和加载时间。

The use of asynchronous requests allows the client's Web browser UI to be more interactive and to respond quickly to inputs, and sections of pages can also be reloaded individually. 异步请求的使用使客户端的Web浏览器UI更具交互性,并可以快速响应输入,还可以分别重新加载页面的各个部分。 Users may perceive the application to be faster or more responsive, even if the application has not changed on the server side. 即使应用程序在服务器端没有更改,用户也可能认为应用程序更快或更敏感。

The use of Ajax can reduce connections to the server, since scripts and style sheets only have to be requested once. 使用Ajax可以减少与服务器的连接,因为脚本和样式表仅需要被请求一次。

State can be maintained throughout a Web site. 可以在整个网站上维护状态。 JavaScript variables will persist because the main container page need not be reloaded. JavaScript变量将持久存在,因为不需要重新加载主容器页面。

CON: CON:

Owing to their dynamic nature, Ajax interfaces are often harder to develop when compared to static pages. 由于其动态特性,与静态页面相比,Ajax接口通常更难开发。

Pages dynamically created using successive Ajax requests do not automatically register themselves with the browser's history engine, so clicking the browser's "back" button may not return the user to an earlier state of the Ajax-enabled page, but may instead return them to the last full page visited before it. 使用连续的Ajax请求动态创建的页面不会自动在浏览器的历史记录引擎中注册自己,因此单击浏览器的“后退”按钮可能不会使用户返回启用Ajax的页面的较早状态,而是可能使它们返回到最后一个状态整页之前访问过。 Workarounds include the use of invisible IFrames to trigger changes in the browser's history and changing the anchor portion of the URL (following a #) when Ajax is run and monitoring it for changes. 解决方法包括使用不可见的IFrame触发浏览器历史记录中的更改,并在运行Ajax时更改URL的锚定部分(后跟#)并监视其更改。

Dynamic web page updates also make it difficult for a user to bookmark a particular state of the application. 动态网页更新还使用户难以为应用程序的特定状态添加书签。 Solutions to this problem exist, many of which use the URL fragment identifier (the portion of a URL after the '#') to keep track of, and allow users to return to, the application in a given state. 存在解决此问题的解决方案,其中许多解决方案使用URL片段标识符(URL中“#”之后的部分)来跟踪并允许用户以给定状态返回到应用程序。

Because most web crawlers do not execute JavaScript code, publicly indexable web applications should provide an alternative means of accessing the content that would normally be retrieved with Ajax, to allow search engines to index it. 由于大多数Web搜寻器都不执行JavaScript代码,因此可公开索引的Web应用程序应提供一种替代的方法来访问通常使用Ajax检索的内容,以允许搜索引擎对其进行索引。

Any user whose browser does not support JavaScript or XMLHttpRequest, or simply has this functionality disabled, will not be able to properly use pages which depend on Ajax. 任何浏览器不支持JavaScript或XMLHttpRequest或仅禁用了此功能的用户,将无法正确使用依赖于Ajax的页面。 Similarly, devices such as mobile phones, PDAs, and screen readers may not have support for the required technologies. 同样,诸如手机,PDA和屏幕阅读器之类的设备可能不支持所需的技术。 Screen readers that are able to use Ajax may still not be able to properly read the dynamically generated content. 能够使用Ajax的屏幕阅读器可能仍然无法正确读取动态生成的内容。 The only way to let the user carry out functionality is to fall back to non-JavaScript methods. 让用户执行功能的唯一方法是退回到非JavaScript方法。 This can be achieved by making sure links and forms can be resolved properly and do not rely solely on Ajax. 这可以通过确保链接和表单可以正确解析并且不完全依赖Ajax来实现。 In JavaScript, form submission could then be halted with "return false". 在JavaScript中,然后可以使用“ return false”暂停表单提交。

The same origin policy prevents some Ajax techniques from being used across domains, although the W3C has a draft of the XMLHttpRequest object that would enable this functionality. 尽管W3C具有可启用此功能的XMLHttpRequest对象的草稿,但相同的原始策略阻止了某些Ajax技术跨域使用。

Like other web technologies, Ajax has its own set of vulnerabilities that developers must address. 像其他Web技术一样,Ajax也具有开发人员必须解决的一系列漏洞。 Developers familiar with other web technologies may have to learn new testing and coding methods to write secure Ajax applications. 熟悉其他Web技术的开发人员可能必须学习新的测试和编码方法才能编写安全的Ajax应用程序。

Ajax-powered interfaces may dramatically increase the number of user-generated requests to web servers and their back-ends (databases, or other). Ajax支持的界面可能会大大增加用户生成的对Web服务器及其后端(数据库或其他)的请求的数量。 This can lead to longer response times and/or additional hardware needs. 这可能导致更长的响应时间和/或额外的硬件需求。

what are the advantages and disadvantages of making ajax calls using jquery? 使用jquery进行ajax调用的优缺点是什么?

It is the way works a single page application (SPA). 这是单页应用程序(SPA)的工作方式。 I would suggest to use a javascript data framework like AngularJS (MVW pattern), Backbone (MVC pattern), KnockoutJS (MVVM pattern). 我建议使用javascript数据框架,例如AngularJS(MVW模式),Backbone(MVC模式),KnockoutJS(MVVM模式)。

You don't have (shouldn't) to duplicate your models in the javascript data layer. 您没有(不应)在javascript数据层中复制模型。 The thing you will loose is the MVC.net view templating thought. 您将失去的是MVC.net视图模板思想。

You can mix both but you then get the both sides drawbacks. 您可以将两者混合使用,但同时会带来双方的弊端。

Here an official microsoft SPA demo built with KnockoutJS : 这里是使用KnockoutJS构建的正式Microsoft SPA演示:

http://www.asp.net/single-page-application/overview/introduction/knockoutjs-template http://www.asp.net/single-page-application/overview/introduction/knockoutjs-template

I am working on a SPA at the moment with all in AJAX (except Authentication), IMO it gives a new great dimension to UX. 目前,我正在使用AJAX(身份验证除外)中的所有组件来开发SPA,而IMO则为UX提供了一个全新的维度。

It's very common practice and I think one of the reason why Microsoft introduced webapi, so you don't really use their mvc but all is dependent on your ajax call, or other js framework, like ember/angular js. 这是非常常见的做法,我认为是Microsoft引入webapi的原因之一,因此您实际上并没有使用它们的mvc,而是全部取决于您的ajax调用或其他js框架,例如ember / angular js。

It takes more time to setup/code ajax way but it performs faster to what user see. 设置/编码ajax方式需要花费更多时间,但是执行起来比用户看到的要快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM