How to use ajax in Grails 3.2.3 - ajax

I worked in grails 2.4.7 .Recently i shifted to grails 3.Previously i used grails custom tag for ajax call like remoteFunction, formRemote .This tags are not available for recent version.Can any one tell me the best way of using ajax for recent version .

Yes, from onward 2.4.x g:remoteFunction has been deprecated in grails.
See this. http://docs.grails.org/2.4.1/ref/Tags/remoteFunction.html
Though, you can always use the javascript/jQuery ajax function as below which is exactly does the same thing.
<g:javascript>
function callMyAjax(){
$.ajax({
url:'${g.createLink( controller:'yourcontroller', action:'youraction')}',
data:{
param1:param1,
param2:param2
}
});
}
</g:javascript>
<input type="button" onclick="callMyAjax()"/>

As I could see, ajax-tags has been deprecated due to inefficiency issues. They recomend to use Ajax or native javascript.
Any way, if you still need them, there is a plugin for grails 3 provided as a migration library, but you can use it aswell:
compile 'org.grails.plugins:ajax-tags:1.0.0.RC1'

Related

Trac Error. Missing or invalid form token. Do you have cookies enabled?

I'm developing a plugin for Trac and trying to submit some info to the database
The scheme is:
Check a user you want to add to a department
Click a button to issue an ajax POST request
Process Request.
Everything was pretty fine while I was working with old 0.11 release. (not sure if that's the reason.
The company I've been working at updated Trac to the current stable 1.0.1 release and something is really wrong now.
Sending POST request without any data like this:
$.post("trac_dep_policy");
Went fine, but if I try to add some data:
$.post("trac_dep_policy", { name: "John", time: "2pm" } );
I get 400 Bad Request error.
After some debugging I figured out it's the protection against CSRF attacks that is working against me. (web/main.py)
The question is simple - how should I deal with it?
CSRF protection is auto-added to each form by a combination of in-place Genshi template modification and read-back on POST request, and I know this has been added very early, fixed version in Trac 0.10.2 release to be clear.
You'll need to use XMLRPC protocol (see XMLRPC plugin) or read the hidden form token yourself.
The answer was simple enough:
Just as #hasienda mentioned in his answer - each form in Trac is provided with a hidden input inside a div element with a certain name tag and a token value:
<form>
<div>
<input type="hidden" name="__FORM_TOKEN" value="9c69c37f52f669fb99b095e4">
</div>
</form>
Now, everything you'll need to do a successful POST request via ajax is to pass this __FORM_TOKEN value together with your data:
var token_value = $("input[name=__FORM_TOKEN]").val();
$.post(url, {__FORM_TOKEN: token_value, data: your_data})

jQuery ajax call and globalEval

I was using the following in an jQuery ajax call just fine with 1.5.1, but since upgrading to jQuery 1.6.2 this globalEval function stops the success function dead in it's tracks. Anybody know why? Or some alternative code that would achieve the same thing?
success: function(data){
...
$(data).filter('script').each(function(){
$.globalEval(this.text || this.textContent || this.innerHTML || '');
});
...
}
Try using 1.6.1. JQuery team noted some Ajax quirks with the latest version and will likely be patching those in 1.6.3

how to make an ajax call in XUL environment

in XUL environment I tried different method using JQuery such as
$.ajax({
url:'http://www.X-site.com/api/call.php?q=test',
success: function(){alert('success');},
error: function(req,str,e){alert(str)}
});
Not getting any callback.... But the call has been made and received on the X-site. This is a X-Site call.
Posted a similar question using jsonGet
How can I make an AJAX call in XUL env., any other means than jQuery ?
It basically does not work with standard jQuery.
But the XUL documentation tells you how to do it :
here

How to check an Ajax request in cakephp?

How to check an Ajax request in cakephp?
Depends on the version of cake.
1.3.x:
$this->RequestHandler->isAjax();
2.x || 3.x
$this->request->is('ajax');
You need to enable the RequestHandler component
var $components = array('RequestHandler');
Then you check if its an ajax request in your controllers with:
$this->RequestHandler->isAjax()
You can find more information about the RequestHandler component here
this question is an older one but just in case anyone comes across this like me and uses CakePHP 2:
RequestHandler::isAjax() is deprecated, use the Request Object's $this->request->is('ajax');
More info here
Without the use of components you can use something like this:
$this->params['isAjax'];
This will return a bool.
this is the way whisch is described is doc.I have been using since i started using cakephp
if($this->RequestHandler->isAjax()){
//
}
If you simply want to check the function of the php side, try:
$this->log('some debug',LOG_DEBUG);
then check app/tmp/logs/debug.log.

Favourite AJAX library for classic ASP?

Just wondering if there are any good server-side libraries for AJAX (prefer JSON rather then XML but anything is good) for classic ASP (VBScript)...
Rather not reinvent the wheel if it's already working and debugged.
Cheers,
Gaspard
EDIT: Server-side ASP VBScript... I have already seen many javascript client side libraries.
Try jQuery. It's amazing!
I am using ajaxed which seems to be one of the few still maintained ajax libraries for classic asp out there. Its working very well for me. It's using prototypejs as its js lib. JSON is fully supported.
You don't really need a server-side library. Accepting POSTs and GETs from AJAX is the same as accepting them the "old fashioned" way. What is key here are good design patterns.
I commonly use a single function to dispatch my simple Ajax calls in Javascript (I use Prototype):
function fetch(elelment,cmd,id) {
//general purpose AJAX function
$(elelment).innerHTML='Loading...<br /><img src="/images/spinner.gif">'
now = new Date()
url = 'http://..../Ajax.asp?CMD='+cmd+'&ID='+pid+'&now='+now
new Ajax.Updater(elelment, url, { method: 'get' });
}
Then on the server side I typically use a select case, break it down by command, fetch the record by the passed ID, and spit out an HTML fragment. I usually build a function to spit out any JSON I need separately.

Resources