1

I'd already posted this to the typeahead github, but I thought perhaps I could ask here and find an answer.

I'm trying to use a remote query as our of our datasources for typeahead. To do so I would want to create a POST and put my query parameters in the payload. When I set the settings.type to POST in the beforeSend function it works, but when I set the settings.data I am not seeing the data being set in my debuggers, and then in my example I use a simple http service which returns your payload, and this also verifies the data is not being sent. Underneath typeahead.js is using jQuery 1.9.1 and in particular using it's ajax beforeSend call. As far as I can tell looking at the jQuery ajax api I am setting the appropriate values in the typeahead beforeSend function.

Here is my code and running in jsfiddle http://jsfiddle.net/75mCa/2/

$('.autocomplete-es .typeahead').typeahead({
    name: 'es-tags',
    remote: {
        url: 'http://httpbin.org/post',
        beforeSend: function (jqXhr, settings) {

            console.log('type is initially : ' + settings.type);
            console.log('data is initially : ' + settings.data);

            settings.type = 'POST';
            settings.data = { fields: ['tags.tag'], query: { prefix: {  tag: 'emp' } } }
            //settings.contentType = 'application/json; charset=utf-8';

            console.log('type is now : ' + settings.type);
            console.log('data is now : ' + settings.data);

            //settings.processData = false;
            //settings.traditional = true;

            return true;
        },
        filter: function (data) {
            console.log(data.data);
            console.log(data.data.fields[0]);
            //do actual processing...for now just looking for this not to throw an error
        //    return data.hits.hits[0].fields.tags.tag;
        }
    }
});

Thanks for any help, G

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Gavin Heasley
  • 13
  • 1
  • 3
  • Could it be that once created, a `jqXhr` cannot change from `GET` to `POST`? If that's the case, why not simply pass the query in `GET` request, and use `replace` to create your url? – Nitzan Shaked Sep 21 '13 at 17:09
  • @NitzanShaked, the request is being sent as a POST, so that isn't the case. Although perhaps it is a case of where the payload cannot be changed? Then again from examples I've seen it should be. Also inspecting my changes with .ajaxSend() seems to confirm things are set as I had hoped. So between that point and when the call is constructed and executed is where I'm loosing the payload. Oh and unfortunately a GET with url parameters is insufficient for my use case. – Gavin Heasley Sep 23 '13 at 13:35
  • I think that's exactly it: you can change a GET to POST, but you can't change the data. At least it didn't work for me, and it's not obvious from the docs that you can. Why isn't a GET good enough? – Nitzan Shaked Sep 23 '13 at 14:24
  • I guess I could set the payload on a GET, just feels dirty. Either way the setting of the payload is my issue, that is, I set it but it doesn't send to the server. The [jQuery](http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings) api seems to indicate its a mutable field. Plus there are other questions on SO and elsewhere dealing with the topic of setting the payload. – Gavin Heasley Sep 25 '13 at 16:56
  • My point was different: if a GET is ok for you, you don't even have to use `beforeSend`. Just use `replace`. – Nitzan Shaked Sep 25 '13 at 18:46

1 Answers1

3

I have this exact same use case (integrate typehead.js with elasticsearch), and ran into the same problem. The problem ends up being that the line of code in jquery that is checking whether or not you have form data to POST is:

xhr.send( ( s.hasContent && s.data ) || null );

... and so just go ahead and set s.hasContent=true in beforeSend and everything works.

mcbridets
  • 46
  • 2
  • That totally worked thank you! Is hasContent just an undocumented field? I had yet to step through the jQuery code, but according to the [docs](http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings) it wasn't an option. Seems like an unfortunate check for our situation. Again, thank you. – Gavin Heasley Sep 26 '13 at 17:53
  • Forked your fiddle from here : http://jsfiddle.net/75mCa/2/ Added just "settings.hasContent" even doesnt post... : http://jsfiddle.net/4UzNe/ Any working example? – EGurelli Oct 21 '13 at 17:41
  • Where do you set s.hasContent for true ? in jquery file ? – Aysennoussi Jan 12 '14 at 18:09
  • Didn't work for me. Struggling with setting payload so it appears on server. Shouldn't be this hard. – t.durden Apr 12 '21 at 14:09