用于http请求响应的Eventlistener

时间:2017-09-28 08:45:41

标签: javascript xmlhttprequest

我需要一个eventlistener来通知用户他们对API的重新申请状态。

        switch (category) {
            case 'B':
                console.log('creating basis');
                break; 
            case 'B-T':
                console.log('creating basis top');
                kladdService.create_B_T(order); 
                break;
            case 'B-M-T':
                console.log('creating basis, mellom, top');
                kladdService.create_B_M_T(order); 
                break; 
            case 'B-M-M-T':
                console.log('creating basis');
                kladdService.create_B_M_M_T(order);
                break;

            case 'Mix_Minimum2B-M-T':
                kladdService.create_Mix_Minimum2B_M_T(order); 
                break;
            case 'Mix_Minimum2M-T':
                kladdService.create_Mix_Minimum2M_T(order); 
                break; 
        }
        console.log('order ble sent: '); 
        console.log(order); 
        //trenger en if her som venter på en http 200 response
        var url = 'http://localhost:32605/api/Kladd/' + category;
        console.log(url);
        var xhr = new XMLHttpRequest();
        console.log(xhr.status); // 0 so either unsendt or opened
        logService.info('prduktet har fått en ID'); 
    };

switch case工作正常,但处理后的create方法的状态只能从devtools中的网络面板中看到。

我假设我需要xhr变量,并且基于xhr.status通知用户结果。

以下是devtools网络面板的输出

info from network panel

1 个答案:

答案 0 :(得分:0)

引用从kladdService服务返回的id变量,我得到了所需的结果:

     switch (category) {
            case 'B':
                console.log('creating basis');
                break; 
            case 'B-T':
                console.log('creating basis top');
                $scope.produktKladdId = kladdService.create_B_T(order); 
                break;
            case 'B-M-T':
                console.log('creating basis, mellom, top');
                $scope.produktKladdId = kladdService.create_B_M_T(order); 
                break; 
            case 'B-M-M-T':
                console.log('creating basis');
                $scope.produktKladdId = kladdService.create_B_M_M_T(order);
                break;

            case 'Mix_Minimum2B-M-T':
                $scope.produktKladdId = kladdService.create_Mix_Minimum2B_M_T(order); 
                break;
            case 'Mix_Minimum2M-T':
                $scope.produktKladdId = kladdService.create_Mix_Minimum2M_T(order); 
                break; 
        }
        console.log(order); 

        if ($scope.produktKladdId) { // if this is not defined, an error appears
            console.log($scope.produktKladdId); 
            logService.info('prduktet har fått en ID'); 
        } else {
            logService.error('noe gikk galt');
        }