1
.factory('ChartService', ['$http','$q',
        function ChartService($http,$q) {
            // interface

            // implementation
            var canceler = $q.defer();

            function getTableData() {

                return  $http.post('http://202.429.115.52:9906/oo/api.php?request=getSubfunctionWiseHCAndSW').success(function (data) {
                    if (data.mm == "No Data Available"){
                        localData();
                    }
                    return data;


                }).error(function(error){
                    alert("error")
                   canceler.resolve();
                    localData()
                  //  console.log(error)
                });



                }

            function localData(){
                        alert("loaddata")
               return  $http.get('vro/hcswc.json').success(function(response){
                    console.log(response+"==");
                    return response;
                }).error(function(error){
                    console.log(error);
                });
            }




            return {
                getTableData:getTableData
            }
        }
    ]);

Error XMLHttpRequest cannot load http://192.127.215.52:9906/api.php?request=getSubfunctionrection. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. The response had HTTP status code 404.

App.js // resolve Code

.state('app.vro', {
        url: "/vro/:isfirstActiveState",
        views: {
          'menuContent': {
            controller: "vrobCtrl",
            templateUrl: 'vrob/vrob.html'


            }
        }  ,
          resolve: {
              tableData:function(ChartService){

                  return ChartService.getTableData();
              }



          }
      })

Hello I am trying to get data form service .I have one condition while calling webservice .the condition is if I will get error then I get request from local in other words if I get any error from server than I need to read json file from local . I need to use resolve and call my service and use that data on controller .my localData is not returning data to resolve .what is the problem in my code ..

here is one example http://plnkr.co/edit/0y9V0m2hmsUBRXoeyjig?p=preview I am getting error correct but it should load data from local json .why it is not loading data from local json

user944513
  • 12,247
  • 49
  • 168
  • 318
  • Looks like you need to enable CORS on the server you are trying to get data from. What language is the server running on? – Ben Aubin Sep 27 '15 at 15:41
  • I am just reading json flle ..but in angular we can read json file – user944513 Sep 27 '15 at 15:43
  • @penne12 question has nothing to do with CORS...it's how to manage returning another promise if first one is rejected – charlietfl Sep 27 '15 at 15:44
  • Yeah. I know - but browsers need a CORS to read data - it prevents weird hacks from happening. Where is this page hosted (url)? If it's not on `http://202.129.215.52:9906`, you'll need to tell us what server you use. – Ben Aubin Sep 27 '15 at 15:45
  • @charlietfl Yeah - I know, but he will always have an error on his server if he doesn't resolve CORS. – Ben Aubin Sep 27 '15 at 15:46
  • @charlietfl so what is solution if first promises is rejected – user944513 Sep 27 '15 at 15:46
  • @penne12 you are not understanding the question how to resolve when first request is rejected – charlietfl Sep 27 '15 at 15:46
  • I think in this situation you will not be able to return the first `$http` and will have to use `$q` and resolve if first request is successful and return second request if not – charlietfl Sep 27 '15 at 15:46
  • Please provide example with plunker as I give one plunker please use that plunker .and edit it.But situation is that i need to use resolve – user944513 Sep 27 '15 at 15:47
  • in the `$http.error of server call` you need to use `return $http.get('path.to.json')` – harishr Sep 27 '15 at 16:03

1 Answers1

1

It is an anti-pattern to use $q to manage $http requests but in this situation I don't know another way:

function getTableData() {

  var deferred = $q.defer();

  $http.post('http://202.129.215.52:9906/mondelez/api.php?request=getSubfunctionWiseHCAndSW')
    .then(
      // resolve callback
      function(data) {
        deferred.resolve(data.data)
        return data.data;
      },
      // reject callback make different request
      function(err) {
        $http.get('data.json').success(function(response) {
          deferred.resolve(response);              
        }).error(function(error) {
          deferred.reject()
        });

      })

  return deferred.promise;

}

DEMO

charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • Where did you read that it's an anti pattern? There are certainly benefits to only passing one type of promise throughout the application. – soote Sep 27 '15 at 17:49
  • @soote `$http` already returns a promise http://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it – charlietfl Sep 27 '15 at 18:05
  • The http promise has a different interface than the q promise, meaning you now need to know which type of promise you are dealing with. – soote Sep 27 '15 at 18:15