weixin_37476846
loubobooo
2017-04-21 04:46

后台代码返回json数据没有接收到?

50
  • java
  • json
  • javascript

这是JS代码
var loading = new Loading();
var layer = new Layer();
$('Account').onclick = function(e){
var newProducts = products.map(function(arr){
return {'id':arr.id,'number':arr.num};
});
console.log(newProducts);
var ele = e.target;
layer.reset({
content:'确认购买吗?',
onconfirm:function(){
layer.hide();
loading.show();

                var xhr = new XMLHttpRequest();
                var data = JSON.stringify(newProducts);
                xhr.onreadystatechange = function(){
                     if(xhr.readyState == 4){
                            var status = xhr.status;
                            if(status >= 200 && status < 300 || status == 304){
                                var json = JSON.parse(xhr.responseText);
                                if(json && json.code == 200){
                                    loading.result('购买成功',function(){location.href = './account.html';});
                                    util.deleteCookie(name);
                                }else{
                                    alert(json.message);
                                }
                            }else{
                                loading.result(message||'购买失败');
                            }
                        }
                };
                 xhr.open('post','/api/buy');
                 xhr.setRequestHeader('Content-Type','application/json');
                 xhr.send(data);
            }.bind(this)
        }).show();
        return;
};
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

7条回答