简体   繁体   English

如何使用Angular.js的$ http发送同步请求

[英]How to send Synchronous Requests with $http of Angular.js

hey guys I know this issue posted a lot, but nothing doesn't help me that is why I asking this question.Question is I am facing an issue of sending a synchronous request to php. 大家好,我知道这个问题发布了很多,但是没有什么对我有用的,这就是为什么我问这个问题。问题是我面临着向php发送同步请求的问题。 here is my Model function which is sending request. 这是我的Model函数,正在发送请求。

State.pushData = function () {
    $http({
    method: 'POST',

    url: 'pushData.php?action=pushdata',
    data: {'status': 'push', 'email' : State.campemail},
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).success(function(response){
        if(response.error){
          console.log(response.error);
          return;
        }
        State.getCartData();
        State.selectedItems = [],
    });
}

this pushData function send a post request to defined url. 此pushData函数将发布请求发送到已定义的url。 and fetch a response. 并获取响应。 the code written is suppose to execute "State.getCartData()" function on success of request sent initially. 假定编写的代码在最初发送的请求成功后执行“ State.getCartData()”功能。 But this is not working in this way. 但这不能以这种方式工作。 both request executes at once. 这两个请求都立即执行。 I had tried $http with .post and then methods but same results. 我曾尝试过$ http和.post,然后尝试方法,但结果相同。 like this 像这样

   State.pushData = function () {
    $http.post('pushData.php?action=pushdata',
    {'status': 'push', 'email' : State.campemail}
    ).then(function(response){
        if(response.error){
          console.log(response.error);
          return;
        }
        State.getCartData();
        State.selectedItems = [],
    });
}

I want to send request asynchronously, that once pushQuote request completes after that getCartData() function will execute. 我想异步发送请求,一旦执行getCartData()函数后,pushQuote请求完成。 please share your experience on this. 请分享您的经验。 thanks in advance. 提前致谢。

got an answer to my question after some brainstorming. 经过一番集思广益,我的问题得到了答案。 I return $http in my model and call .then() on returned response. 我在模型中返回$ http,并在返回的响应上调用.then()。 it worked as I want to send request once first is completed successfully. 它的工作原理是我想在第一次成功完成后发送请求。 Here is my model function 这是我的模型功能

State.pushData = function () {
  return $http.post('pushData.php?action=pushdata',
  {'status': 'push', 'email' : State.campemail}
  );
}

in above function I just send post request to server and return its response to controller function. 在上面的函数中,我只是将发布请求发送到服务器,并将其响应返回给控制器函数。 which executes right after returning from model. 从模型返回后立即执行。 here is my controller function. 这是我的控制器功能。

scope.pushIt = function() {
  var responseObj = State.pushData();
  responseObj.then(
    function() { //successs call back
      /*Business logic*/
      State.getCartData();
      State.selectedItems = []
    },
    function() { //Error call back
      /*Business logic*/
    }
  );
}

Beauty of this approach is you can use then method as many as you want. 这种方法的优点在于,您可以根据需要使用then方法。 they will all execute one by one in chain. 他们将一一执行。

scope.pushIt = function() {
 var responseObj = State.pushData();
  responseObj.then(
  function() { //successs call back
   /*Business logic*/
   },
  function() { //Error call back
   /*Business logic*/
  }
 ).then(
   function() { //successs call back
   /*Business logic*/
   },
   function() { //Error call back
    /*Business logic*/
   }
 );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM