AJAX and redirection

2 Messages Forum Options Options
Embed this topic
Permalink
Kram.V
AJAX and redirection
Reply Threaded More
Print post
Permalink
Hi all

I have an AJAX action page that validates an email address and sends some feedback text to the callee.

On a valid email address, I want to perform a server side redirect to a different page. Is this doable? My redirection is not working from the AJAX action page.

Following is the code for the Action page
import com.xxx.utils.EmailAddress;

public class GetStartedAction extends Page {
       
        public String feedbackText;
       
    public void onGet() {
   
    String emailId = getContext().getRequest().getParameter("emailId");
    if(!EmailAddress.isValidText(emailId)){
   
    feedbackText = "Email is invalid";
   
    }else{
   
    this.setRedirect("/users/register.htm");
    }
    }

    public String getContentType() {
    return "text/xml; charset=UTF-8";
    }

}
sabob
Re: AJAX and redirection
Reply Threaded More
Print post
Permalink
Hi,

Kram.V wrote:
>
> I have an AJAX action page that validates an email address and sends some
> feedback text to the callee.
>
> On a valid email address, I want to perform a server side redirect to a
> different page. Is this doable? My redirection is not working from the AJAX
> action page.


Redirect won't work from the server side when using Ajax. You will
have to use Javascript in this scenario:

window.location=...


If you are using an Ajax library such as Prototype and their function
AjaxRequest, you could return the following in your Velocity template:


#if ($redirectUrl)
  <script type="text/javascript">window.location=$redirectUrl</script>
#else
   // You content comes here
   $feedbackText
   ...
#end


And your Page should be something like this:

public MyPage extends Page {

   public void onGet() {
    ...
    Context context = getContext();
    String contextPath = context.getRequest().getContextPath();
    String redirectUrl = "users/register.htm";
    redirectUrl = context.getResponse().encodeRepsonseUrl(redirectUrl);
    addModel("redirectUrl", redirectUrl);
   }

   public String getContentType() {
     return "text/javascript; charset=UTF-8";
   }
}

What happens here is that Prototype notices from the contextType that
you are returning javascript. So it will execute the script, in this
case window.location=...

Note you should probably not mix javascript with other content in your
Velocity template. So place the redirect code inside an #if statement
so only the <script>...</script> is returned in the response.

Hope this helps.

kind regards

bob


>
> Following is the code for the Action page
> import com.xxx.utils.EmailAddress;
>
> public class GetStartedAction extends Page {
>
> public String feedbackText;
>
>     public void onGet() {
>    
>     String emailId = getContext().getRequest().getParameter("emailId");
>     if(!EmailAddress.isValidText(emailId)){
>    
>     feedbackText = "Email is invalid";
>    
>     }else{
>    
>     this.setRedirect("/users/register.htm");
>     }
>     }
>
>     public String getContentType() {
>     return "text/xml; charset=UTF-8";
>     }
>
> }
>


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Click-user mailing list
Click-user@...
https://lists.sourceforge.net/lists/listinfo/click-user