16

We pull data from server and for that If we are using Struts, then we can pull either by submitting a page which MVC Architecture or we cam make an AJAX call but conventions is of using forms and render response but we also face challenges to give rich user experience, so we compromise convention and start using excessive AJAX, so how we should make balance between two?

Roman C
  • 47,329
  • 33
  • 60
  • 147
Vardan Gupta
  • 3,265
  • 5
  • 26
  • 39

6 Answers6

34

I personally think that AJAX should be used for displays updates and form submissions should be done via a page reload. Reasoning?

When submitting forms, you are telling the application to do something. Users tend to want to feel that it was done. When a page doesn't reload, users are often left wondering "Did that work?". Then they have to check to make sure what they did was right.

On the other hand, when you are displaying a chart or something, and the user says to "display 2011 data....now 2012 data" for instance, they aren't "doing" something (creating new entities, sending emails, etc). So AJAX can provide a nice user interface in this case. Page reloads would be annoying here.

In conclusion, I think form submission should be done via page reloads (let the user see it working), whereas display updates should use AJAX (prevent annoying page reloads).

Of course, this is a preference thing. Some of my company's applications use AJAX all over. But those are the applications that are the most difficult to maintain and debug. ;)

Snowy Coder Girl
  • 5,162
  • 10
  • 36
  • 65
  • 29
    You might want to reconsider the way you think about ajax. Would you really want the page to reload when you upvote, or make a comment on SO? ajax done right is clean and degrades gracefully. (Additionally, a dated aspect, but significant nonetheless, especially with mobile devices: ajax can save bandwidth.) –  Nov 08 '12 at 16:56
  • 4
    I agree. I was talking more "form submission" like filling out a form. If your change isn't massive, I agree to use AJAX. I was thinking more "add all this data to the database", etc – Snowy Coder Girl Nov 08 '12 at 16:58
  • Good though process to discuss this problem, I was thinking along the same lines but had to look elsewhere for other peoples thoughts, thanks reputation++ – cwiggo Mar 12 '14 at 16:41
9

Regular old HTML form submission and fancy ajax forms are not mutually exclusive.

First, make the plain HTML form work correctly. Then, add javascript to hijack the form and send an ajax request.

The controller and model don't care if the user's browser supports (or has enabled) javascript. The rendered view is decided by whether the call was made with javascript or a simple form submission. This is one of the strengths of the MVC pattern, not a constraint.

7

I think that the choice between the two is somewhat intrinsic:

  • a form submission is synchronous and it reloads the page.

  • an ajax call is asynchronous and it does not reload the page.

If a certain action will change a lot of UI elements or needs to poll a lot of data to be rendered, I would go with form submission. On the other hand, if a certain action is used for simple actions, like populating a select box or improving user experience, then I would go for an AJAX call.

There is nothing avoiding you to use as many ajax calls or form submissions as you need, so in the end is up to you.

Th0rndike
  • 3,360
  • 3
  • 19
  • 39
2

If you have errors between submissions of data the only forms method you could check on the server. On the other hand if you make Ajax calls you could check that errors on the client side. So, out of this different technologies of transmitting data we could follow a decision that they serve different purposes.

Roman C
  • 47,329
  • 33
  • 60
  • 147
1

In this day and age, there is virtually no case to use the old standard HTML form submission method (other than pure nostalgia, or perhaps not knowing).

The <form> tags themselves can still be useful if you want to take advantage of the .serialize() function (which grabs all name-data pairs within the form into a query string), but other than that we don't need to use <form> tags at all these days.

Using AJAX, the developer has more control over the entire process, in a more condensed code base. But more importantly, we just don't do things that way anymore.

Consider:

(Old Style - Forms) When an HTML form submits: (a) it gathers the form field name= attribute values (these become the defacto variable names) (b) together with user-entered data in the form fields (which become the variable values), and posts these data-pairs to a PHP file (as specified in the action= attribute on the form tag). THEN, the page changes to that other page, causing a noticeable refresh of the screen and loss of all previously inputted user data. Even using the trick action="", wherein the form data is posted back to the same page it started from, the page is still reset/refreshed.

(New Style) Exactly the same process can easily be programmed using javascript/jQuery - except that the page does not refresh. All previously entered user data/text can remain undisturbed.

Back to your question:

With the old-style HTML form submission, the page changes - so if you want to do field validation you use javascript/jQuery to cut into the submit process, thus:

$('myform#btnSubmit').click(function(){
    var some_fields_failed = false;
    //check form field values here, set: some_fields_failed = true
    if (some_fields_failed){
       return false; //halts the HTML Form Submit process and returns control to the user
    }
});

in which case you already are using most of the AJAX construct that would replace the HTML form submission process.

This simple introduction to AJAX provides some compelling reasons to use AJAX instead of the old-style HTML Form Submission process:

AJAX is a developer's dream, because you can:

Update a web page without reloading the page
Request data from a server - after the page has loaded
Receive data from a server - after the page has loaded
Send data to a server - in the background


Note that you can write ajax code in pure javascript, but until very recently it was considerably simpler (much less typing, more consistent) to use the jQuery (javascript) library. Hence, all examples above use jQuery. To include jQuery in your project, it is only necessary to include a reference to the jQuery library:

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
</head>

StackOverflow maven, Schabse Laks, created a simple jQuery tutorial that is worth stepping through (IMPORTANT: Use Down Arrow to step through the pages)

cssyphus
  • 31,599
  • 16
  • 79
  • 97
  • Now that the [fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) is implemented in most, if not all, major browsers, I'd argue it's just as great to use pure javascript for AJAX requests as using jQuery. Don't import jQuery just for AJAX! – Adrian Wiik Aug 07 '19 at 11:45
0

When sending a form with AJAX, you generate the POST request and not the browser, so you have more control over it. Even if you don't need that control to begin with, in time it might become necessary.

One case would be protection against CSRF attacks on forms. It can be implemented by adding a hidden form input field containing a CSRF token, which is sent together with the form data. But a preferred implementation would be to add a custom header to the submitted POST request. However, you can't do the latter when using the old form submission method - the browser composes the request and you can't add your own headers.

Bar
  • 11
  • 2