0

I have two files.

location.php, that outputs this:

[["javascript"],["PHP"]] 

and in another file:

<script type="text/javascript">
$.getJSON('location.php', function(data) {
      var sampleTags = [];

      $.each(data, function(key, val) {
         sampleTags.push(val);
         });

         alert(sampleTags); // show javascript, php


        //-------------------------------
        // Preloading data in markup
        //-------------------------------
        $('#myULTags').tagit({
            availableTags : sampleTags, // this param is of course optional. it's for autocomplete.
            // configure the name of the input field (will be submitted with form), default: item[tags]
            itemName : 'item',
            fieldName : 'tags'
        });
    });
</script>

the auto complete doesn't work. Why ?

if i use:

var sampleTags = [ 'javascript', 'php'];

all works well, but with json the autocomplete simply doesn't work.

2 Answers 2

3
$.each(data, function(key, val) {
  sampleTags.push(val[0]);
});

should reduce [["foo"],["bar"]] to ["foo", "bar"]

Sign up to request clarification or add additional context in comments.

Comments

2
[["javascript"], ["PHP"]]

is a two dimensional array. Your Javascript is expecting a one dimensional array. Have your PHP output:

[ "javascript", "PHP" ]

In PHP the array should look like this:

array( "javascript", "php" );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.