对于状态codeS承诺解决 [英] For which status codes promise resolving

查看:178
本文介绍了对于状态codeS承诺解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要澄清的HTTP状态codeS的承诺是解决并为其拒绝,我该怎么理解它只能解决200的情况下,其余被拒绝

I want to clarify for which http status codes promise is resolving and for which is rejecting, how I understand it resolving only for 200 case and the rest is rejecting

这是什么?或者我们有一些其他情况?

it is ? or we have some other cases ?

推荐答案

您可以决定的话,承诺为例

you can decide of it, exemple of promise

var deferred = $q.defer();
        $http({
            method: 'POST',
            url: url,
            data: data,
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}

        })
                .success(function(data, status, headers, config) {
                    deferred.resolve(data);
                })
                .error(function(data, status, headers, config) {
                    var error = [data, status, headers, config];
                    deferred.reject(error);
                    console.log('Error: ' + error);
                });
         deferred.promise.then(function(result) { 

         }, function(reason) {
            alert('Failed: ' + reason);
          });

在此code,首先你实例化的承诺

In this code, first you instantiate the promise

 var deferred = $q.defer();

第二个定义哪里是成功的。

Second you define where is success

deferred.resolve(data);

三的失败

deferred.reject(error);

最后,你写你的,当他们中的一个被称为函数

And finally you write your function for when one of them are called

deferred.promise.then(function(result) { 

         }, function(reason) {
            alert('Failed: ' + reason);
          });

因此​​,如果您添加

so if you add

.success(function(data, status, headers, config) {
                 if(status == 200) {
                    deferred.resolve(data);
                 }
                })

将返回你只答应当你有HTTP code 200

it will return you promise only when you have http code 200

这篇关于对于状态codeS承诺解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆