1

I need to call a external URL (not in my development server) that returns JSON response. Doing some research I found this and this post and tried almost every, right now my code is this:

$("#query").on("keyup", function(e) {
    if (e.which !== 32) {
        var value = $(this).val();
        var noWhitespaceValue = value.replace(/\s+/g, '');
        var noWhitespaceCount = noWhitespaceValue.length;

        if (noWhitespaceCount % 3 === 0) {
            var request = $.ajax({
                type: 'GET',
                data: "text=" + $(this).val(),
                url: "http://192.168.0.159:3000/products/search/all",
                success: function(data) {
                    console.log(data);
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    console.log(jqXHR, textStatus, errorThrown);
                    request.abort();
                }
            });
        }
    }
});

But it's not working, so what is the best way to achieve this?

Community
  • 1
  • 1
Reynier
  • 2,420
  • 11
  • 51
  • 91
  • JSONP would probably be the best. – Kevin B Aug 27 '13 at 16:20
  • You need to narrow your question WAAAYY down. Start with just running an Ajax call on load or something (just the .ajax) part and see if that fails or succeeds, edit your question once you do that, and we'll go from there – Tommy Nicholas Aug 27 '13 at 16:20
  • have you tried setting breakpoints in your script using browser developer tools such as Ctrl+Shift+I in Chrome? If you are hitting the endpoint, try setting contentType: "application/json" – Jim Frenette Aug 27 '13 at 16:21
  • @TommyNicholas I did it already and it returns `200 OK 12ms` so call is going on – Reynier Aug 27 '13 at 16:22

1 Answers1

2

How many times you bind the "keyup" ? Have you tried abort an previous request?

$("#query").on("keyup", function(e) {
    if (e.which !== 32) {
        var value = $(this).val();
        var noWhitespaceValue = value.replace(/\s+/g, '');
        var noWhitespaceCount = noWhitespaceValue.length;

        if (noWhitespaceCount % 3 === 0) {

         /* Aborting previous Requests */
         if(request) request.abort();

            var request = $.ajax({
                type: 'GET',
                data: "text=" + $(this).val(),
                url: "http://192.168.0.159:3000/products/search/all",
                success: function(data) {
                    console.log(data);
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    console.log(jqXHR, textStatus, errorThrown);
                    request.abort();
                }
            });
        }
    }
});