Home > Web Front-end > JS Tutorial > Nodejs implements obtaining the product classification of a certain product_node.js

Nodejs implements obtaining the product classification of a certain product_node.js

WBOY
Release: 2016-05-16 15:57:34
Original
1324 people have browsed it

I had nothing to do, so I was researching the e-commerce website. It happened that a friend was opening a certain treasure store, and I was very happy to see the dazzling variety of product categories. So I did a little research.

Getting product categories is all done by AJAX, and because I need to log in, I have no trouble, so I took the simplest method, go to the background and directly open the console, paste the code in and run it, hehe, all categories are run to CAT. in the data.

Rookie practicing, please give me some advice.

var CAT = {
  //[{id: '', name: '', data: [{id: '', name: '', data:[{id: '', name: ''}]},{}, ...]}, {} ...]
  data: [],
  url: function(){
    return 'http://upload.taobao.com/auction/json/reload_cats.htm?t='+Math.random();
  },
  init: function(){
    var url = CAT.url(),
      post_data = 'path=all';
    CAT.ajax(url, post_data, CAT.first_r);
  },
  first_r: function(data){
    var rs = data[0]['data'],
      first_l, first_d, i, j, second_id, second_d, func;
    for(i=0;i<rs.length; i++){
      //保存一级分类
      first_d = rs[i]['data'];
      first_l = [];
      for(j=0; j<first_d.length; j++){
        //保存二级分类同时查询三级分类,并提供存储数据的容器
        second_id = first_d[j]['sid'];
        second_d = {
          'id': first_d[j]['sid'],
          'name': first_d[j]['name'],
          'spell': first_d[j]['spell'],
          'data': []
        };
        first_l.push(second_d);
        func = CAT.second_r(second_d['data']);
        CAT.ajax(CAT.url(), 'path=next&sid='+second_id, func);
      }
      CAT.data.push({
        'id': rs[i]['id'],
        'name': rs[i]['name'],
        'data': first_l
      })
    }
  },
  second_r: function(container){
    return function(data){
      if(data.length<1){
        return
      }
      var rs = data[0]['data'],
        i, j, here, third_d;
      for(i=0; i<rs.length; i++){
        third_d = rs[i]['data'];
        for(j=0; j<third_d.length; j++){
          here = third_d[j];
          container.push({
            'id': here['sid'],
            'name': here['name'],
            'spell': here['spell']
          });
        }
      }
    }
  },
  ajax: function(url, post_data, func){
    var xhr = new XMLHttpRequest(),
    result;
    xhr.open('POST', url, true);
    xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xhr.send(post_data);
    xhr.onreadystatechange=function(){
      if (xhr.readyState==4 && xhr.status==200){
        result = JSON.parse(xhr.responseText);
        func(result);
      }else if(xhr.readyState==4 && (!xhr.status==200)){
        console.log('Ajax Return Error!');
      }
    }
  }
};
CAT.init();
Copy after login

The above is the entire content of this article, I hope you all like it.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template