views:

48

answers:

1

So I have this one CakePHP web application that I'm trying to refactor.
There is a large portion of code that usually works like this:
E.g. "Flag":
User clicks flag button
Redirect the user to the flag action
Flag action flags post
Redirect user to post that was just flagged.

I am also trying to enhance it with JavaScript like so:
User clicks flag button
Javascript makes AJAX request to that action
Call returns with a status that JS inteprets

Is there some way to streamline this sort of flow so that it is easily testable and can be covered with a javascript that doesn't need redirection?

+1  A: 

Lookup jQuery's .post() function. That lets you post to a URL and handle (in a callback function) whatever results the server returns. The server can return some simple data to indicate success or failure, or it can return an HTML fragment to be placed somewhere on the page (under control of your Javascript code), or really anything else you want.

Pointy
Would this still work if the page that javascript called redirects when its finished?
chustar
Well the redirect will be something you'll see in the response header, I'm pretty sure. Your client-side code would have to deal with it. It would probably be easier to construct a server-side service behind the URL that *knows* it's being called via XMLHttpRequest (ajax) instead of trying to re-use an existing mechanism.
Pointy