-1

i have an auto-suggest url from that i need to write a JavaScript code through which i will be able to see the auto-suggest data. i tried the below code but i am not able to get through it.

<!DOCTYPE html>
<head>
<script>
    var xmlhttp = new XMLHttpRequest();
    var url = "http://***.poc.xxxxx.com/v1/staples/suggest?authKey=baef7f8e39c512342c8a14b7f6018b58&q=wat&rows=8";
    var words = []
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            myFunction(xmlhttp.responseText);
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();

function myFunction(response) {
    var data = JSON.parse(response);
    var req_data = data.suggestions[0].suggestion;
    console.log(req_data);
    //document.getElementById("id01").innerHTML = words;
}
</script>
 </head>
   <body>
     <!-- <div id="id01"></div> -->
   </body>
</html>

the thing i am getting in response is:-

{"suggestions":[{"suggestion":"\u200B\u200B\u200B<b>wat</b>er","categories":[{"name":"Water & Juice","filter":"category_id%3A4606"},{"name":"Water Dispensers & Filters","filter":"category_id%3A16896"}]},{"suggestion":"\u200B\u200B\u200B<b>wat</b>er cooler","categories":[{"name":"Water Dispensers & Filters","filter":"category_id%3A16896"},{"name":"Kitchen Storage & Organization","filter":"category_id%3A1303"}]},{"suggestion":"\u200B\u200B\u200B<b>wat</b>er bottle","categories":[{"name":"Lunch Totes & Water Bottles","filter":"category_id%3A8812"},{"name":"Water & Juice","filter":"category_id%3A4606"}]},{"suggestion":"\u200B\u200B\u200B<b>wat</b>er cups","categories":[{"name":"Disposable Plates & Cups","filter":"category_id%3A992"},{"name":"Disposable Cups","filter":"category_id%3A13302"}]},{"suggestion":"\u200B\u200B\u200B<b>wat</b>er bottle labels","categories":[{"name":"Labels","filter":"category_id%3A997"},{"name":"Mailing & Shipping Labels","filter":"category_id%3A6118"}]},{"suggestion":"\u200B\u200B\u200B<b>wat</b>er dispenser","categories":[{"name":"Water Dispensers & Filters","filter":"category_id%3A16896"},{"name":"All Kitchen","filter":"category_id%3A60479"}]},{"suggestion":"\u200B\u200B\u200B<b>wat</b>ch","categories":[{"name":"Pedometers & Fitness Trackers","filter":"category_id%3A2554"},{"name":"Smart Watches","filter":"category_id%3A62030"}]},{"suggestion":"\u200B\u200B\u200B<b>wat</b>ercolor","categories":[{"name":"Abstract Art","filter":"category_id%3A12645"},{"name":"Wall Art/Decor","filter":"category_id%3A26678"}]}]}

from that response i need to find all the product name which coming after suggestion not suggstions like suggestion for wat water cooler etc.

s_m
  • 83
  • 1
  • 1
  • 9
  • Please describe a specific problem. "not able to get through it" does not describe where you got stuck, what you observed, what error you saw, what result you got, what exactly you want help with, etc... – jfriend00 Mar 04 '15 at 04:14
  • @jfriend00 first i need to store all the auto-suggest query from that url in to an array. that i am not able to do. – s_m Mar 04 '15 at 04:16
  • @jfriend00 i have now mentioned. can you please give me some link where i can get auto-suggest by parsing url using javascrit – s_m Mar 04 '15 at 04:23
  • OK, now that you show a sample response, what exactly do you want to extract from that response? Do you want the list of suggestion words? the list of category names, list of filter words? – jfriend00 Mar 04 '15 at 05:06
  • @jfriend00nyes i want that. – s_m Mar 04 '15 at 05:14
  • See my answer to see if it is what you want. – jfriend00 Mar 04 '15 at 05:15
  • @jfriend00 i want data [water,watercooler,water bottle,water cups, water bottle labels etc.] – s_m Mar 04 '15 at 05:19
  • We could have been done a long, long time ago (more than an hour ago) if you just put that detail in your question. – jfriend00 Mar 04 '15 at 05:21
  • @jfriend00 i am sorry for that. this is a small part of my work. i need to write a javascript after getting that data.whether i am getting autosuggest like that or not. – s_m Mar 04 '15 at 05:24
  • When you write a question here, you must specify EXACTLY what you want the result to be, otherwise we have to try to guess what you want, most people will not try to help and it will take a lot of time for people to figure out what you want. Showing the exact data structure you want to finish with is an EXCELLENT way to be clear about the result you want. – jfriend00 Mar 04 '15 at 05:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72197/discussion-between-s-m-and-jfriend00). – s_m Mar 04 '15 at 05:29
  • @jfriend00 look i am not getting any answer on that question. help me. – s_m Mar 04 '15 at 09:00

1 Answers1

1

It is hard to discern what exactly you're asking for. If what you want is just a list of all the "name" properties that are returned as suggestions, you could collect those like this:

function myFunction(response) {
    var data = JSON.parse(response);
    var items = data.suggestions;
    var names = [], cat;
    // iterate array of suggestions
    for (var i = 0; i < items.length; i++) {
        cat = items[i].categories;
        // iterate array of categories in each suggestion
        for (var j = 0; j < cat.length; j++) {
            names.push(cat[j].name);
        }

    }
    console.log(names.join(","));
}

Working demo: http://jsfiddle.net/jfriend00/trdppth0/


Now that you've clarified what output you want, you can get the list of suggestion words like this:

function myFunction(response) {
    var data = JSON.parse(response);
    var items = data.suggestions;
    var suggestions = items.map(function(item) {
        return item.suggestion;
    });
    console.log(suggestions.join(",")); 
}

Working demo: http://jsfiddle.net/jfriend00/bv3yfkwr/

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • no i don't want the categories word . i want the suggestion word. – s_m Mar 04 '15 at 05:16
  • @s_m - why didn't you say that when I asked my question above BEFORE I wrote this code? It is very, very hard to answer a question that doesn't say what you really want. – jfriend00 Mar 04 '15 at 05:18
  • i am sorry . i was trying to explain that. – s_m Mar 04 '15 at 05:22
  • @s_m - second code example added to my answer that produces a list of suggestion words. – jfriend00 Mar 04 '15 at 05:23
  • i am getting data like water i want to remove those tag. how can i do that – s_m Mar 05 '15 at 07:38
  • @s_m - I supplied an answer to your [other question](http://stackoverflow.com/questions/28875087/remove-the-html-tag-using-javascript-from-html-page/28889388#28889388) on removing the HTML tags. – jfriend00 Mar 05 '15 at 22:55
  • yea i got it. but i did that with regex. – s_m Mar 06 '15 at 04:46
  • @s_m - a regex is not foolproof for HTML. It simply isn't possible to parse HTML reliably with only a regular expression. It can work for simple things. And, if you had already selected an answer to that other question, then please mark it with the green checkmark so those of us who come along later know that we shouldn't spend time thinking up a new answer. When no answer is given a green checkmark, that tells the community that you haven't yet seen an acceptable answer. FYI, once you have 15 reputation points (which you do), you can upvote all helpful answers too. – jfriend00 Mar 06 '15 at 04:49
  • upvote my question too. all people down voting them. i don't know why. – s_m Mar 06 '15 at 04:55
  • @s_m - questions often get downvoted when they are hard to understand what is really being asked. That was certainly true on this question (I had to ask you many questions before I could figure out what you were actually asking). It is your job when asking a question to be 100% absolutely clear what you are asking and exactly what result you want to achieve. Show input data structures. Show code you tried. Show desired output data structures. People should not have to ask multiple questions in order to figure out what your question means. – jfriend00 Mar 06 '15 at 05:00
  • FYI, you will also attract many more answers if your question is completely clear. Here's a StackOverflow guideline on asking a good question: http://stackoverflow.com/help/how-to-ask and http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/ – jfriend00 Mar 06 '15 at 05:02
  • yea you are write thanks. from now i will do that. can i have your e-mail.? – s_m Mar 06 '15 at 05:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72384/discussion-between-jfriend00-and-s-m). – jfriend00 Mar 06 '15 at 05:05