Add ajax calls to a folder in Zend - ajax

Is there a way to create an Ajax folder inside application, so that I can then add each controllers ajax as a seperate controller in that ajax folder?
For instance, say I have IndexController, EmployeeController, MemberController. Inside Ajax folder, I want to have the same three names as well.
Then If I want to call an ajax action, I could refer to /ajax/member/whatever.
Is this possible with zend?

You could set up routes to create urls like that, but I wouldn't duplicate the controllers as you're suggesting. One controller can handle both html and ajax requests. You could include a format parameter in your url like this: /member/21/format/ajax
Then the member viewAction can check the format variable. If it's ajax, you can disable your layout and return the ajax response.

Related

MVC pattern: js code that is calling server via ajax part of view or controller?

I know that the webpage that is opened by the user is part of the view. This one is easy, but what about js code calling the server to retrieve data via ajax? Is this part of the controller or still the view?
Can the view request data like this?
Thank you very much for any help.
Javascript, including Ajax, is a client-side technology. Therefore, in the context of MVC, any js script must be part of a template file.
Note that a template file is not the view (the V in MVC), but a part of it. The view should incorporate both server-side components (classes, interfaces, etc) and client-side components (html, js, css, images, etc). For example, an instance of a view class could read some data from the domain model and then load and render a certain template file, injecting the fetched data into it (formatted), in order to be printed on the screen.
So, an ajax object should be defined in a template file. Its request to the server is handled either by the controller, or by the server-side components of the view, depending on the MVC approach that you are choosing to implement. Though, the server response should always be created by the server-side components of the view.

Scala Play - calling controller method from view doesn't load a new page

I display information, gather some user's input, and call a controller method using ajax/java script call from the view. However, instead of displaying a new/different html page, the controller method returns back to the original html page that I called jscript from.
Is this because of ajax call? How do I make sure that controller method would display the html file it's supposed to, instead of staying on old html page?
It sounds to me like what you want to do is to open a different url than the one you are on. To do that you do not use ajax. Instead try using javascript to change window.location to tell the browser to go to the url (or 'call your controller'). It looks like this:
window.location.assign("/your/controller/url"); // or
window.location = "/your/controller/url";
There is more to read about it here: https://developer.mozilla.org/en-US/docs/Web/API/Window.location
You can think of ajax as a separate little browser that lets you call the web server but letting you handle what comes back with javascript rather than always showing it to the user. So if you want to do the call with ajax but get the response HTML into your page, you are responsible for taking that response and inserting it into the DOM.

How to add a fileupload method in my existing mav controller to accept ajax file upload submit

My situation is: I have a Spring MVC project is running now, I need to include a file in normal form object to submit to server side to handling.
I did quite lots research recently, I found there is no way to submit a file in a regular form as file upload submit form has to configured to support file encode (enctype="multipart/form-data"). it is impossible in regular form.
Ajax provides a way to do the file uploading in javascript by javascript function call, but they need a individual servlet to listen to the javascript call. My project already did everything in modelandview controller to handle form object and model object logic.
My question is: May I add a file upload method in my modelandview controller to listen to my Ajax function call? If it is true, what's looks like?
Please help!!
After a while works on this issue, I found some thing is very interesting. If I want to use some thing else in my Spring MVC controller. I have change my controller class to "extends MultiActionController", which means make the controller to support multiAction, and then you can add other class and action in Spring MVC modelandview style controller, even you can use "webservlet" or "ResponseBody" style controller functions in your MVC controller, you don't need create other new controller to support Ajax controller, you only need update your existing controller by adding some functions. it is much easy to manage you existing code too.

Spring with AJAX integration

So I'm able to successfully integrate AJAX requests with Spring MVC. However, I have a problem- if I click the "submit" button of my form, my #Controller class detects the url and returns a ModelAndView. However, what I want is that there be an AJAX check first, and if the form submission is not successful (e.g., blank fields), return an AJAX response. Otherwise, proceed as per normal and display a ModelAndView. However, I have no clue how to integrate both at the same time.
Any ideas or tutorials are appreciated.
Thanks!
You have several choices:
submit the form to a specific, different URL, when using AJAX
add a specific parameter to the request when posting using AJAX, and use this specific parameter to check if the request is n AJAX request
test if the X-Requested-With request header is present and contains XMLHttpRequest
I would go the PJAX route or what's also known as HiJax.
Basically you return a subset of the page if it's an AJAX request using headers. Most people than just use conditions in their view/template to decide to include the full or chrome-less HTML.

Transfer request to MVC controller or View

I have an http handler which picks up all requests for my custom extension .page . This works fine with old fashion asp.net where I simply do a Server.Transfer to the aspx file of choice.
However, I would like to move this to MVC and instead reroute to either a Controller or a View. Doesn't matter which though I would prefer to a Controller.
Any help on this subject is appreciated.
Thanks!
I'd go about adding a new entry in the RouteTable.Routes ignored routes:
routes.Ignore("{*allcustomextension}",
new {
allcustomextension = #".*\.ext(/.*)?"
});
This will allow the handler to be invoked as a physical resource.
To redirect from the handler context to a View use:
System.Web.Mvc.UrlHelper.Action("ViewName", "ControllerName");
Firstly, you should redirect only to a controller which will select the view to render but think that you don't need that custom handler. Try something like that (you should change it somehow to match the url format you have)
routes.MapRoute("page", "{controller}/{action}.page");
If that doesn't work and you really want to redirect from your handler, combine the ignoring route from Xander with
Response.Redirect(System.Web.Mvc.UrlHelper.Action("ActionName", "ControllerName"));
Afaik, Server.Transfer works only with asp.net classic so your only option is a redirect.

Resources