0

I have the following ajax request

  <script>

$(document).ready(function(){

          $.ajax({
        url:'obtenerusuarios',
         dataType:'json',
        type:'get',
        cache:true,
        success:json
         });

         function json(data){
          console.log(data);
         }      

    });

</script>

This Ajax request return the list of users and I want to change the results of this response into javascript variable. I need to replace the availableTags array with the new data in response

  <script>
  $(function() {
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
    ];
    $( "#tags" ).autocomplete({
      source: availableTags
    });
  });
  </script>
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
jc1992
  • 644
  • 3
  • 10
  • 24
  • possible duplicate of [Submit array param with jQuery ajax/load](http://stackoverflow.com/questions/3942408/submit-array-param-with-jquery-ajax-load) – Medet Tleukabiluly Aug 25 '15 at 18:09
  • Are you using a translator? (I just want to help you convey your question better) – Manatax Aug 25 '15 at 18:10
  • @simon I assume he means request – Manatax Aug 25 '15 at 18:14
  • is the question related to autocomplete through ajax ? Because even if you point new array to the same variable, autocomplete will be using the old array (which it was initialized with). – coding_idiot Aug 25 '15 at 18:17
  • Yes , I am using a translator , I try to write better. – jc1992 Aug 25 '15 at 18:18
  • 1
    Check the manual: http://jqueryui.com/autocomplete/#remote-jsonp (click the "view source" button at the bottom). This example is for JSONP, but it should work for you if you change the `dataType` to `'json'`. – gen_Eric Aug 25 '15 at 18:22
  • @jcsa Was you question answered? – Manatax Oct 17 '15 at 20:30

1 Answers1

0

Provided the return from that ajax request is an array, you can try this:

<script>
    var insertar = function(data){
        // Maybe clean 'data' here

        $( "#tags" ).autocomplete({
            source: data
        });
    }

    $(document).ready(function(){
        $.ajax({
            url:'obtenerusuarios',
            dataType:'json',
            type:'get',
            cache:true,
            success:insertar
        });
    });
</script>

You might need to clean the "data" inside "insertar" to adjust to what you want.

Manatax
  • 4,083
  • 5
  • 30
  • 40