0

I am new to Angular and need to download data into a service. It works fine with local json file; however, obviously you want to get the data from another URL which then gives the issue of cross domain download. Is there a way to go around this? I need to download the data from here http://files.parsetfss.com/c2e487f5-5d96-43ce-a423-3cf3f63d9c5e/tfss-31564b7d-6386-4e86-97c5-cca3ffe988f3-phones.json rather than 'phones/phones.json' below.

'use strict';

/* Services */

function makeArray(Type) {
  return function(response) {
    var list = [];
    angular.forEach(response.data, function(data) {
      list.push(new Type(data));
    });
    return list;
  }
}

function instantiate(Type) {
  return function(response) {
    return new Type(response.data);
  }
}


angular.module('phonecatServices', []).
  factory('Phone', function($http){
    var Phone = function(data){
      angular.copy(data, this);
    };

    Phone.query = function() {
      return $http.get('phones/phones.json').then(makeArray(Phone));
    }

    Phone.get = function(id) {
      return $http.get('phones/' + id + '.json').then(instantiate(Phone));
    }

    // Put other business logic on Phone here

    return Phone;
  });

Can this be put in the following query from parse.com (how can I write the http request bit to fit into Angular.

var query = new Parse.Query("coursesParse");
query.find({
  success: function(results) {
   },
  error: function(error) {
  }
});
Amir
  • 1,667
  • 3
  • 23
  • 42
  • Did you try the solution here ?http://stackoverflow.com/questions/23823010/how-to-enable-cors-in-angularjs – Tasnim Reza Oct 19 '15 at 06:45
  • I used php curl to get data. Dont know if that is any good for you – el3ien Oct 20 '15 at 01:48
  • I'm not exactly clear what the question is here. Are you saying that when you put the URL you want to download from in, you get an error? – Claies Oct 20 '15 at 01:49
  • or better, if you control the server, you can fix that cross domain issue – el3ien Oct 20 '15 at 01:51
  • Claies, I use parse.com cloud server to retrieve data. when I replaced 'phones/phones.json' with the url from parse to retrieve the data, I get error message in line to cross domain http request. I could get the data through the parse API but I dont know how to fit Angular into that to ensure the service is built after the data is loaded. – Amir Oct 20 '15 at 01:55

1 Answers1

1

You can do it this way.

    Phone.query = function() {

    var query = new Parse.Query("test");
    query.find({
      success: function(results) {
          //makeArray(Phone(results));
         for (var i = 0; i < results.length; i++) {
              var object = {
              "age": results[i].get('age'), 
              "carrier": results[i].get('carrier'), 
              "id": results[i].get('id1'), 
              "imageUrl": results[i].get('imageUrl'), 
              "name": results[i].get('name'), 
              "snippet": results[i].get('snippet')
            };
            makeArray(Phone(object));
         }

       },
      error: function(error) {
      }
    });
 }
Asieh hojatoleslami
  • 3,240
  • 7
  • 31
  • 45