How to access the XmlHttpRequest object in JSF ajax's addOnEvent callback? - ajax

I want to add a header to every AJAX request sent from every form of every page in a JSF application. In jquery I can use something like this:
$(document).ajaxSend(function(ev, xhr, opt) {
xhr.setRequestHeader('custom-header', random-value);
});
But how can I do something similar in JSF 2.2?
jsf.ajax.addOnEvent(function(data) {
// how to access the XmlHttpRequest object?
});

Related

is there any way to get the request data in Primefaces ajaxStatus like jsf.ajax.addOnEvent

I want to modify All Ajax request data .
i am using p:ajaxStatus for monitor ajax requests .
is there any way to get the data like below for primefaces ajax status
jsf.ajax.addOnEvent (function( data )){
console.log(data) ;// here i can view and modify data
});
---------------------------------------------------------
<p:ajaxStatus onstart="jsfun()" onsuccess="jsfun()" /> //is there any way to
// get data like above
To modify requests' data:
<script>
$(function() {
$(document).on('pfAjaxSend', function(event, xhr, source, errorThrown) {
xhr.pfSettings.data += '&my_param=my_value';
});
})
</script>
Obligatory warning: pfAjaxSend is an undocumented event, not a part of the official API. AFAIK there's no way to do this without relying on this event or some implementation details. I did check that pfAjaxSend is present in PF 5-6 though.
In case you're still interested in ajaxStatus' arguments, you can get them like this:
<p:ajaxStatus onstart="console.log(arguments);" onsuccess="console.log(arguments);" />
Inspect the arguments in your browser's dev tools.

angularjs changes in data not affected in views ajax

I am developing an application with angular js
Question: When I have an ajax call to server and I need to change the views based on the result of ajax call, the views don't get affected by this call, I think it the page renders before ajax call is finished but I don't know how to resolve it
For example the following piece of code
$scope.addItem = function() {
$http({
method :'GET',
url : 'addItem',
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
}).success( function(data) {
$scope.allItems = data;
});
}
the allItems changed after the ajax call but the view is not changed
how should I solve this?

Magento ajax form validation

I have a form in Magento that I build in code, and that works with ajax, which I need to validate.
I would like to be able to use Magento's built-in validation functionality, but I don't know how I would trigger it since the form is not submitted. The data is retrieved via ajax and outputted in a list below the form.
Is there someone who can point me in the right direction?
Thanks in advance.
Edit:
This is the javascript code used to hande the ajax request. Its called by the onclick event of the button.
function advancedtranslateSearch(url){
new Ajax.Request(url, {
method: 'get',
parameters: $('search_form').serialize(),
onSuccess: function(transport) {
json = transport.responseText.evalJSON();
$('result').update('<div class="hor-scroll">'+json.records+'</div>');
}
});
}
You should use form's onsubmit event.
To prevent page from reloading you must return false value from your function.

how to do ajaxSetup beforeSend like stuff in DOJO toolkit to provide CSRF token in DOJO AJAX POSTs

Recent releases of Django require csrf_token to be sent along with each POST request (whether through AJAX or through normal request).
Flaw in CSRF handling fixed.
They suggest that, Django will now accept the CSRF token in the custom HTTP header X-CSRFTOKEN, as well as in the form submission itself, for ease of use with popular JavaScript toolkits which allow insertion of custom headers into all AJAX requests.
They give an example to do this in jQuery
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
// Only send the token to relative URLs i.e. locally.
xhr.setRequestHeader("X-CSRFToken",
$("#csrfmiddlewaretoken").val());
}
}
});
I am not able to figure out how to do something similar in DOJO toolkit.
I have been using dojo.rpc.JsonService() for a while.
Please suggest a way to do something similar in DOJO?
Or the only option is to do this on each and every xhrPost request separately?
Disclaimer: I'm not familiar with django.
Outside of extending the base dojo xhr definition(wouldn't recommend it), I'd say you may want to extend dojo.xhr or build a utility method.
Utility method would mix in or set the "headers" attribute of the arguments you pass into dojo.xhr :
myCustomNameSpace.xhr = function (xhrArgs) {
var csrfHeader = { "X-CSRFToken" : dojo.byId(csrfmiddlewaretoken).val(); };
xhrArgs.headers?dojo.mixin(xhrArgs.headers,csrfHeader):xhrArgs.headers=csrfHeader;
dojo.xhrPost(xhrArgs);
};
Invoked via myCustomNameSpace.xhr({method:"POST",url:"http://www....."});
The {% csrf_token %} will appear in the form just like this:
<input type='hidden' name='csrfmiddlewaretoken' value='...' />
so just mix your content like this:
var form = dojo.byId("postForm");
dojo.xhrPost({url:form.action,
content:{text:form.text.value, csrfmiddlewaretoken:form.csrfmiddlewaretoken.value},
)

create a nested jQuery post in another post callback?

since I'm new in jquery, can you tell me how to redirect a page to another action method ?
I develop a MVC web application. I use jquery post method to do some validation, and when it return true, it will be redirect page to another one.
My problem is..when when I redirect page using window.location, it's works well in IE (IE 9). but didn't work on firefox & chrome.
So, I try to using jquery post method to redirect page from action method in my controller. I call redirect post method in a jquery post call back.
it is my code :
$.post(posturl, formData, function (result) {
if (result == 'True') {
$.post("/Controller/RedirectMethod", {_Action: 'Index', _Controller: 'Home'}, null);
}
else
alert('failed');
}
);
and this is my RedirectMethod :
[HttpPost]
public ActionResult RedirectMethod(string _Action, string _Controller)
{
return RedirectToAction(_Action, _Controller);
}
so how I should create a nested post in another post callback ?
or there is another way to redirect page ?
thanks,
You won't be able to do a RedirectToAction inside an Ajax request.
If you need the web page to change to a different location inside the Ajax response, use window.location as Ben suggested.
One thing to bear in mind is that you'll need to remove the 'HttpPost' action filter.

Resources