Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hook and change ajax request

Tags:

jquery

ajax

proxy

I'm new the JQuery. I am using a closed framework, that allows me to add code, but not to change some of the behavior. In order to add functionality, I need to hook into a specific Ajax request and either redirect it to my URL (so i can change the data there and be a proxy), or change the parameters of the request at the client side.

Is this possible with JQuery? And if so, how?

The original event is fired after a text input box is being updated.


Update:

After further inspection, it seems the textbox is manipulated using the JQuery Autocomplete plugin. I am trying to translate the words returned from this ajax request. Basically the user enters text in one language, and i want to translate that to english (the translation itself isn't the problem) so the autocomplete will use the english words, and then i want to translate from english to the original language, to be displayed in the text box and the hovering div.

like image 903
Amirshk Avatar asked Feb 10 '10 08:02

Amirshk


2 Answers

I have used the jQuery autocomplete plug-in to do something similar, although not in a scenario where I am dealing with a "closed framework".

You will be able to easily accomplish what you want by using the .setOptions() method on the autocomplete object after it has been initialized. So your code will go something like this (untested! pseudocode!):

$(autoCompletedField).setOptions({
   formatItem: function(resultsRow, resultPosition, totalResults, inputWord) {
      // translate your menu items to be displayed in the drop down menu here
      // perhaps extend jQuery to handle $.translate(inputWord) ?
      return "<li>"+translatedWord+"</li>";
   }
}).result(function(event,selectedItem){
   // optionally do something more when the user selects one of those items here
   // translate the selectedItem back to another language? i dont know.
   return translatedItem;
});

check out the options: http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions

like image 137
Jordan Sitkin Avatar answered Nov 11 '22 12:11

Jordan Sitkin


I don't think there is a simple solution to what you're trying to do since the XMLHTTPRequest object is pretty closed down for security reasons.

If you're lucky the part that does the AJAX request is broken out into a function that you can overwrite or override using the prototype object.

If not, you're probably better off hooking the change event of the textbox.

In jQuery:

$('#textboxid').unbind();
$('#textboxid').change(function () {
    $.ajax({
         url: 'http://yourwebservice',
         data: {}, //the data to send to your webservice
         success: function (data) {
             //do your magic
         };
    })
});
like image 3
Oscar Kilhed Avatar answered Nov 11 '22 10:11

Oscar Kilhed