How to use the data from an AJAX call

secdave

New Member
#1
I have the following JQuery AJAX call which successfully returns data from the database -
Mã:
$.ajax({
   url : 'get_hashtags.php',
   type : 'GET',
   success : function(data){
      var obj = jQuery.parseJSON(data);
      console.log(obj);
   }
});
This is the data returned -
Mã:
(2) [{…}, {…}]
0
:
{category: "ht.austria"}
1
:
{category: "ht.landscapes"}
length
:
2
__proto__
:
Array(0)
Now I'd like to use the data returned from the database as the suggestions in the following Javascript code. So instead of the "suggestions" like ht.austria it should use the data returned from the DB.
Mã:
$('#inputTags').amsifySuggestags({
    suggestions: ['ht.austria','ht.landscape'],
    classes: ['bg-warning','bg-warning'],
});
Unfortunately I'm not sure how to do this. I've tried calling a function in the success part of the Ajax call, but getting an error message that amsifySuggestags is not a function. Can anyone help please?

Update
I've now tried with the following code as suggested by Shiv, but I still get an error -
Mã:
$.ajax({
   url : 'get_hashtags.php',
   type : 'GET',
   success : function(data){
      var obj = jQuery.parseJSON(data);
      console.log(obj);
      var suggestionsArr = [];

      obj.forEach(function(item){
        suggestionsArr.push(item.category)
      });

      createHashtags(suggestionsArr);
   }
});

function createHashtags(data)
{
    $('#inputTags').amsifySuggestags({
        suggestions: data,
        classes: ['bg-warning','bg-warning'],
    });
}
This is the error I'm getting -
Mã:
Uncaught TypeError: $(...).amsifySuggestags is not a function
at createHashtags (upload.php:1557)
at Object.success (upload.php:1551)
at i (VM392 jquery-3.2.1.min.js:2)
at Object.fireWith [as resolveWith] (VM392 jquery-3.2.1.min.js:2)
at A (VM392 jquery-3.2.1.min.js:4)
at XMLHttpRequest.<anonymous> (VM392 jquery-3.2.1.min.js:4)
 

Admin

Administrator
Thành viên BQT
#2
Extract category data by looping obj and push into an Array and use.

look into code snippe.
Mã:
var obj = [
  {category: "ht.austria"},
  {category: "ht.landscapes"}
];

var suggestionsArr = [];

obj.forEach(function(item){
  suggestionsArr.push(item.category)
});


console.log('suggestionsArr : ', suggestionsArr);


   /* $('#inputTags').amsifySuggestags({
    suggestions: suggestionsArr,
    classes: ['bg-warning','bg-warning'],
}); */
 
Top