简体   繁体   中英

How to send response ajax into variable javascript

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM