开发者

Should I go with AJAX and JQuery's complexity and speed or GET's simplicity?

If a user tries to log in and the login fails the page should display an error message to the user. There are two main ways I see to do this: use a form action on the HTML page and in the php script if the login information is incorrect redirect with header to the login page with a $_GET value like loginfailed. The login page would check for this value and if it exists it would display the error.

The second way I see to do this is not use a form at all and instead use JQuery to capture the submit button press and use AJAX to determine if an error occurs. The php file would echo back a status and the javascript file would interpret it and if it was loginfailed, it would use JQuery to append the error message on to the page.

Now I will go over what I feel the pros and cons of each method are.

Method 1 Pros:

  • Very simplistic with no need for JQuery, Javascript, and AJAX.
  • The error status is displayed within the URL as well.

Method 1 Cons:

  • Since there is a header call, a redirect is necessary. Also, the login page must be reloaded. It is a small page but it is a reload nonetheless.
  • The status message is displayed in the URL. This means that users can type in status messages in to the URL and receive error messages on the page for errors that did not actually occur. Is this a problem? Maybe. Maybe not.

开发者_开发技巧Method 2 Pros:

  • Since it is using AJAX, there is no need to load another URL and thus, no extra page is loaded.
  • This method uses JQuery to update the page with the error message so no redirect is necessary.
  • The error status is not displayed in the URL.

Method 2 Cons:

  • Much more complex than the first solution.
  • An external javascript file is needed and must be loaded every time the login page is accessed regardless of whether or not it is used.
  • The default behavior of the submit button is overridden and annulled. Its only behavior comes from its interaction with the javascript file.

What would SO do? I would like to stay away from answers such as "it depends on how much traffic your site would have" if that would be at all possible.


Always use the simplest solution possible until/unless there's a very good reason to do otherwise. It's better to finish something that's maybe (and maybe not) less than ideal than to deliver something gold-plated eventually, maybe.

Also, I generally prefer to follow a progressive enhancement strategy, such that everything works without Javascript, and then add Javascript to make it work in an improved manner. This has the added benefit of being functional, even when/where Javascript is disabled.


I think you fail to grasp the matter.

  • Login is not something self-sufficient. It is used to change state of the site. But with no reload it will not be changed. So, page reload is required anyway. or user will have to do it manually to get access to the authorized section.

  • Is login the only site feature that uses JQuery/AJAX? If not - why you're worrying about loading this library once, when most likely it will be loaded at every page?

  • There are still clients with JS disabled, for various reasons. A good web application will always let these clients in, even at cost of less functionality.

The latter is the main question, most important one. Why to choose between two? Why not to use both? - one for compatibility and another for usability?

So, I'd suggest to create basic functionality using GET to pass come codes, not messages.
And optionally improve it with some AJAX bells and whistles but with JS-based reload on succesful login anyway

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜