加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 如何在角度js中进行同步http请求

发布时间:2020-12-17 08:08:46 所属栏目:安全 来源:网络整理
导读:如何在角度js中阻止http请求,以便我可以在角度js中的下一行使用$ http响应。 这里$ http对象不会在下一行返回结果,以便我可以在fullcalender上传递一个javascript库的$ http响应。 这里$ scope.data返回空值。 下面的示例代码 $http.get('URL').success(fu
如何在角度js中阻止http请求,以便我可以在角度js中的下一行使用$ http响应。

这里$ http对象不会在下一行返回结果,以便我可以在fullcalender上传递一个javascript库的$ http响应。

这里$ scope.data返回空值。

下面的示例代码

$http.get('URL').success(function(data){

$scope.data = data;

});

$.fullCalender({
data: $scope.data
});
您可以使用 promises。

这里是一个例子:

$scope.myXhr = function(){

    var deferred = $q.defer();

    $http({
        url: 'ajax.php',method: 'POST',data:postData,headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        })
        //if request is successful
        .success(function(data,status,headers,config){

            //resolve the promise
            deferred.resolve('request successful');

        })
        //if request is not successful
        .error(function(data,config){
            //reject the promise
            deferred.reject('ERROR');
        });

    //return the promise
    return deferred.promise;
}

$scope.callXhrAsynchronous = function(){

    var myPromise = $scope.myXhr();

    // wait until the promise return resolve or eject
    //"then" has 2 functions (resolveFunction,rejectFunction)
    myPromise.then(function(resolve){
        alert(resolve);
        },function(reject){
        alert(reject)      
    });

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读