Calling from JavaScript to server-side code in JSF
I am looking for an easy way to call a bean method that will not take any parameters and will not return a string in JSF. What I don't really need is that the method returns the result of the action and then uses the entire JSF lifecycle to translate me to a different view. I need to do this with JavaScript so that I can put together some parts of the application on the client side, and moving to the A4J RichFaces part still hasn't gotten me anywhere.
So here's the script again in step-by-step form:
- from JS issues GET to some address
- on a server process that GET and returns JSON or HTML (mostly a string)
- Once the request is sent back to the client, I want him to be able to process it further using JS.
Thanks!
a source to share
Use a4j: jsFunction and data attribute.
So roughly you want something like:
<button onclick="callBackend();">Go</button>
<a4j:jsFunction name="callBackend" action="#{myBean.someMethod}" data="#{myBean.someString}" oncomplete="handleResponse(data);"/>
<script>
function handleResponse(response) {
alert(response);
}
</script>
a source to share
Damo: can you explain why it can only work the first time the callBackend method is run? I am experiencing strange behavior where the first call succeeds and the next calls just block. I can see the server-side code is executing, but some weird result is sent back to the browser (something like _viewstate, etc.).
a source to share