typeahead.js icon indicating copy to clipboard operation
typeahead.js copied to clipboard

Same data (simple string array) works with remote and local, but not with prefetch

Open calexo opened this issue 13 years ago • 13 comments

Hi ! Same data (simple string array) works with remote and local, but not with prefetch.

["Corse","Alsace","Vall\u00e9e de la Loire","Jura \/ Savoie","Provence","Languedoc - Roussillon","Beaujolais","Sud Ouest","Bourgogne","Bordeaux","Champagne","Vall\u00e9e du Rh\u00f4ne"]

JS error in isArray, in jquery.min.js. TypeError: invalid 'in' operand e

Using JQuery 1.9.1

Any idea ? Thanks !

calexo avatar Feb 22 '13 09:02 calexo

I was able to get this to work…

Screen Shot 2013-02-22 at 8 52 00 AM

Screen Shot 2013-02-22 at 8 52 39 AM

So I'd guess this is potentially browser-specific? What browser are you using?

timtrueman avatar Feb 22 '13 16:02 timtrueman

Well, as I said, this does work for local, and for remote, but not with prefetch. Thanks for your help

calexo avatar Feb 22 '13 17:02 calexo

Ahh misread.

timtrueman avatar Feb 22 '13 17:02 timtrueman

I tried with prefetch in Chrome 24.0.1312.57 and it worked. @calexo do you see this error on page load or after you type something into the input? Post a stack trace if you can. Also, what browser are you using?

jharding avatar Feb 23 '13 08:02 jharding

It's on page load. I also use Chrome 24.0.1312.57.

Uncaught TypeError: Cannot use 'in' operator to search for '184' in ["Corse","Alsace","Vall\u00e9e de la Loire","Jura \/ Savoie","Provence","Languedoc - Roussillon","Beaujolais","Sud Ouest","Bourgogne","Bordeaux","Champagne","Vall\u00e9e du Rh\u00f4ne"] jquery-1.9.1.min.js:3
M jquery-1.9.1.min.js:3
b.extend.map jquery-1.9.1.min.js:3
processPrefetchSuccess typeahead.js:487
b.isFunction.i jquery-1.9.1.min.js:3
c jquery-1.9.1.min.js:3
p.fireWith jquery-1.9.1.min.js:3
k jquery-1.9.1.min.js:5
r jquery-1.9.1.min.js:5

calexo avatar Feb 23 '13 09:02 calexo

The code below is what I'm using to debug this issue. Can you try it out on your machine and let me know if it works for you? Also, try it out out in incognito mode to avoid caching issues.

index.html

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="http://twitter.github.com/typeahead.js/releases/latest/typeahead.css">
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://twitter.github.com/typeahead.js/releases/latest/typeahead.js"></script>

    <style>
      .container {
        width: 800px;
        margin: 50px auto;
      }

      .tt-dropdown-menu {
        background-color: #fff;
        border: 1px solid #000;
      }

      .tt-suggestion.tt-is-under-cursor {
        background-color: #ccc;
      }
    </style>
  </head>

  <body>
    <div class="container">
      <input class="typeahead" type="text">
    </div>

    <script>
      $('.typeahead').typeahead({
          prefetch: '/test/test.json'
      });
    </script>
  </body>
</html>

test.json

[
  "Corse",
  "Alsace",
  "Vall\\u00e9e de la Loire",
  "Jura \\/ Savoie",
  "Provence",
  "Languedoc - Roussillon",
  "Beaujolais",
  "Sud Ouest",
  "Bourgogne",
  "Bordeaux",
  "Champagne",
  "Vall\\u00e9e du Rh\\u00f4ne"
]

jharding avatar Feb 23 '13 10:02 jharding

Well... It works ! So I went on in investigating, trying to find the differences... And I found ! My JSON was actually generated, and didn't get correct mime type (text/hml instead of application/json), resulting in this crash.... Thanks a lot for your help, and really really sorry !!

calexo avatar Feb 23 '13 10:02 calexo

What's strange and prevented me to find that sooner is that it does work with remote ;)

calexo avatar Feb 23 '13 10:02 calexo

I'm actually having this same(/similar?) issue --

I set up a test case here:

  • live: http://bl.ocks.org/rmosolgo/raw/5020372/99c29f1d225f2c38b86828e8badcc416e67fc6d7/
  • source code: https://gist.github.com/rmosolgo/5020372/

I'm loading every possible combination of data: local/prefetched/remote strings and objects. You can see each dataset from the links. Also, each datum has "({{ source }} {{data type }})" in its value, so you can see in the dropdown where it's coming from. Each datum is unique -- no value appears twice.

But I'm only seeing local data -- am I doing something wrong?

Checking my Chrome console, I see:

  • both prefetched json files load successfully with Content-Type: application/json in the response header
  • my localStorage only has data for __codes_strings_local and __codes_objects_local
  • if I type something which is only present in one of the remote data files (eg "Education"), I see that purpose_codes_strings_remote.json (which is the second remote source) loads successfully with Content-Type:application/json in the response header

Ok -- I hope this is helpful. This library looks awesome and I'm really looking forward to figuring it out and making magic happen!

rmosolgo avatar Feb 23 '13 16:02 rmosolgo

Also I just made an alternative test case where the data is loaded in 2 sets:

    $('#typeahead').typeahead(
        [
            {
                name: "codes_strings",
                local: local_codes_strings,
                prefetch: "purpose_codes_strings_prefetched.json",
                remote: "purpose_codes_strings_remote.json"
            }, {
                name: "codes_objects",
                local: local_codes_objects,
                prefetch: "purpose_codes_objects_prefetched.json",
                remote: "purpose_codes_objects_remote.json"
            }                   
        ]       
    )

http://bl.ocks.org/rmosolgo/raw/5020372/alternative.html

rmosolgo avatar Feb 23 '13 17:02 rmosolgo

@rmosolgo the files you're trying to load are JavaScript objects and not valid JSON – this causes jQuery to throw a parse error when it tries to load them. You need to use double quotes and wrap keys in quotes. After you do that run your files through http://jsonlint.com to make sure they're valid and then try again.

jharding avatar Feb 23 '13 19:02 jharding

aaaand we're in business. sorry bout that.

rmosolgo avatar Feb 24 '13 03:02 rmosolgo

No worries. Seeing as both @rmosolgo and @calexo's issues have been resolved, I'm going to close this issue.

jharding avatar Feb 24 '13 03:02 jharding