In my Web Application I have jqGrid and other struts2 jquery plugins , now what i want is to have have a global error handler function that will redirect to my index page if error occurs any idea how can I implement this functionality.
You can use Interceptor to manage your exception on application level. Also see this document for exception-handling in struts.
You can also use global-results to fulfill your needs
If you handle exceptions by redirecting to index page it will only behave in desired manner when request are generated by browser directly rather than Javascript asking browser to generate request using Ajax. e.g. by clicking on anchor tag or directly accessing application through URL. In these conditions only you will get redirect to index page.
Ajax is used to send data to, and retrieve data from, a server asynchronously (in the background) without interfering with the display and behavior of the existing page on client side. Javascript responsible for calling Ajax has to handle response from server. So you have to manually trigger redirect. Have a look at this related Question.
Related
I'm recently facing the problem with last request url after login. Normally, everything works properly. But if I proceed following procedure I have troubles:
Open an App in one tab and log in
In this tab I go somewhere where AJAX request to the server are proceeded regularly
Open a new tab with the app (I'm still logged-in)
In this tab I log out
In the mean time the AJAX request from the 1st tab is proceeded automatically
with HTTP 401 (cause I've logged-out)
When I try to log in again in the 2nd tab than I receive the JSON of the AJAX request from the 1st tab because it was the last request.
I would suspect that Spring Security would neglect AJAX request for "last request url". Is it possible to set this somewhere? Or is there any good workaround to this?
Thanks,
Mateo
I don't think there is a general way to distinguish ajax requests from "regular" requests initiated by the user navigating a browser. If you can distinguish them in your application (e.g. by mapping ajax requests to specific urls that can be matched against some patterns), you could easily implement what you are looking for. (EDIT: An easier way to identify ajax requests is suggested by Sérgio in his below comment.)
The component that is responsible to perform redirection after a successful login is SavedRequestAwareAuthenticationSuccessHandler, therefore one possibile way to customize the framework's default behavior is to provide your own AuthenticationSuccessHandler, and inject it into the UsernamePasswordAuthenticationFilter. It should be possible to autowire the RequestCache in your class, and decide if you want to replay the last cached request or just ignore it in case it's known to be an ajax request.
In my winphone application I have a Browser control and a page with HTML form in it. Is it possible to somehow extract POST variables from control after user fills in and submits a form inside it?
Trying to execute JS and bind some events to submit button is not an option. I wonder if it can be done by using standard methods and events accessible for Browser control.
The Navigating event would be your most likely option for getting this information, but you can only get the Uri being navigated to. You do not get access to the Request object that is being sent to the server.
I need some help to implement facebook authentication. I'm doing this sample:
http://csharpsdk.org/docs/web/getting-started
and it works fine, I can get the access token and get user's info. But my problem is because javascript start a loop of http post's, because he's executing the javascript who invokes the handler, so infitly redirect for my page.
So I have Main.aspx and is there where I have the javascript implemented and all my main content, if my handlrer redirect to Main.aspx he enter in loop, if I redirect the handler to SecondPage.aspx he stops the loop, but if i click go back to Main.aspx(or someone else where i've the javascript) he redirect to second...
The issues is that you are redirecting back to your original page from your ashx handler page and when you do that your JavaScript is executing again and hence submitting another Post to the server which in turn calls your ashx handler page again. So you end up in an infinite loop. But when you redirect to a different page from your ashx handler page then the JavaScript that authenticates your user does not execute and does not send another Post.
I ran into this same problem myself and came up with a fairly simple solution which I posted on a different thread dealing with the same issue. you can find the answer on this thread.
On a side note, I continued to research this and found a great solution that allows me to do this all server side instead of using the JavaScript SDK, so I'm not sure it would be of interested to you since your post was about "How to make HTTP POST facebook authentication" but in case you are, the link gives a solution dealing with an ASP.NET app that is not written using MVC. Getting Started With The Facebook C# SDK
I need to get the browser URL in Grails, or particularly, a parameter off of it in the UrlMapping.
I need this in a particular circumstance, after an asynchronous call from a different controller/action, at this point, request.forwardURI has changed to the asynchronous url, while the actual browser url has remained the same.
Is there a simple way to do this?
If you have access to the request object, you can get any property of the URL (including parameters), because request is an implementation of HttpServletRequest.
Since you're doing an asynchronous call, just add the current browser URL as a parameter to your request. Retrieve it via window.location and add it to the params passed in via your async call.
If you are trying to keep track of the current state your user is in based on URL parameters you might look into Spring Web Flow support within Grails -> http://grails.org/doc/latest/guide/theWebLayer.html#webflow
Obviously this is more for stateful web apps than RIA stuff with Ajax but Web Flow does a very good job at isolating particular user operations and might help here as well.
I need to be notified any time a largeish asp.net mvc web application makes an ajax call to the server. We're using both jquery, and the built-in Ajax.* methods to do the remote calls, and I would like a global way of knowing when we make a call without having to manually inject some sort of "IsMakingCall" method to every request.
The root problem we're trying to solve is session timeout. If the user leaves a page up and goes to lunch (for example), they get errors when they get back because the ajax call is returning the login page instead of the expected json or partial html result.
My idea was to have a js timer which would be reset any time we make an ajax call. That way, if the timer runs out (ie. their session has now timed out) I can just auto-log them out. This is how sites like bank of america and mint.com work.
Thanks!
I handle the problem you describe in a different fashion, client-side. I have a jQuery plugin on my master page that will prompt the user to renew their session via a dialog just before it is set to expire. If the user doesn't respond (or clicks logout), it calls the logout action on the application. If the user "renews" their session, it makes an AJAX call back to the server, resetting the sliding window server-side.
You can find code on my blog, http://farm-fresh-code.blogspot.com. Right now it doesn't take account of AJAX actions that may renew the session, but you could instrument it so that jQuery AJAX, via a global ajaxStart would reset the timer. It uses ajaxStart to reset the timer when a jQuery AJAX request is made. I don't use MS AJAX much so I'm not sure if there's a global hook you can use. It doesn't appear so from inspecting the code, but you could manually add one via AjaxOptions to each request. You might investigate adding a handler to the Sys.Net.WebRequestManager, too.
Basically, the problem here is the default behavior returns back a login page. The result of that is a 200 OK, which to jQuery is a successful call. Correct this handling for ajax calls from MVC and you should be good.
I think the most straight-forward way to handle this is to change the way the Authorize works for ajax scenarios. You can tell if a request is Ajax at that point, and return a similar status code that ASP.NET doesn't invoke the login redirect for(403). Then jQuery will interpret as a failure and fall into that code on the client script.
You can then easily have a shared function in a common js file to handle a friendly way to tell the user they have expired their session.
Something like this might do it
public class AjaxAuthorize : AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.Result = new ContentResult();
filterContext.HttpContext.Response.StatusCode = 403; // 401 would make ASP.NET return the login page and a 200 OK status
}
else
base.HandleUnauthorizedRequest(filterContext);
}
}
I guess you should create default handler functions for success or begin in the jQuery Ajax framework, depending on when you want the notification to be sent...