Modify ajax request
Modify ajax request
Hi,
I am not sure if this is possible but thought I'd ask. Essentially I want to modify the jQuery.ajax.data property after the event has been triggered but before the request is sent?.
The reason I ask is because I am using jquery-ui autocomplete on a number of fields on a page and the data parameter will need to be modified depending on which input triggered the request, EG if the input field had the name 'code', data would 'code=' + req.term, if the input name is 'name', data would be 'name=' + req.term.
I have tried using a var as the value of $.ajax.data and use PreFilter or beforeSend to alter the parameter but I have not been successful. The ajax'ed part of the function appears to be closed by the time the event is triggered.
I've attached my code below, incase it helps.
- jQuery.noConflict();
jQuery(
function() {
autocomplete_setup();
}
);
function autocomplete_setup() {
client_code_autocomplete( jQuery(".with_autocomplete") );
/* jQuery(".with_autocomplete").each(function() {
client_code_autocomplete( jQuery(this) );
});
*/
}
function client_code_autocomplete(form_selector) {
jQuery.widget("custom.catcomplete", jQuery.ui.autocomplete, {
_renderMenu: function( ul, items ) {
var that = this,
currentCategory = "";
jQuery.each( items, function( index, item ) {
if ( item.type != currentCategory ) {
ul.append( "<li class='ui-autocomplete-category'>" + item.type + "</li>" );
currentCategory = item.type;
}
that._renderItemData( ul, item );
});
}
});
form_selector.catcomplete({
source: function(req, response) {
jQuery.ajax({
//beforeSend: function() {
// get_url(form_selector);
//},
url: '/cgi-bin/search',
data: "fnct=client_code&code=" + req.term,
dataType: "xml",
success: function( xmlDoc ) {
var clients = [];
var Codes = xmlDoc.getElementsByTagName("client");
for( i = 0; i < Codes.length; ++i ) {
var obj = new Object();
obj.label = Codes[i].firstChild.data;
obj.value = Codes[i].getAttribute("id");
obj.type = Codes[i].getAttribute("bus_type");
clients.push( obj );
}
clients.sort(function(a,b) {
if (a.type < b.type)
return -1;
if (a.type > b.type)
return 1;
return 0;
});
response(clients);
}
});
},
minLength:3,
select: function(event, ui) {
form_selector.val(ui.item.value);
if (document.forms[0].phase) {
document.forms[0].phase.value = 4;
}
document.forms[0].submit();
}
});
}
- // Unfortunately, the URL parameter list needs to be configured to allow
// searhcing on, Client Code; mlcod or cod, Company Name; mlname or post code; pstcode
function get_url( form_selector ) {
var input_name = form_selector.attr('name');
console.log("Input name="+input_name);
var url = (input_name == 'mlcod') ? 'fnct=client_code&code=' :
(input_name == 'mlnam') ? 'fnct=client_code&ml_nam=' :
'fnct=client_code&pstcod=';
console.log("Returning "+url);
return url;
}