Place local Json in external Json $http Angular / Ionic

Asked

Viewed 898 times

3

I want to pull this Json data from the Service by an External Json via $http. I found several examples plus they are not returning result to me.

.service('ProdutosService', function($q) {
return {
produtos: [

  {
     "nomeProd": "nomeproduto",
     "codCat": "nomecategoria",
     "id": "01"
  },
  {
     "nomeProd": "nomeproduto2",
     "codCat": "nomecategoria2",
     "id": "02"
  },

],

getProdutos: function() {
  return this.produtos
},

getProduto: function(produtoId) {
  var dfd = $q.defer()
  this.produtos.forEach(function(produto) {
    if (produto.id === produtoId) dfd.resolve(produto)
  })

    return dfd.promise
  }
 }
});
  • Can you explain what problem you’re trying to solve? Click [Edit] and add more details to your post.

  • i have this json products, plus it’s within the service. I want to pull this json from an external file, using $http.

  • You want to take this product data from a server?

  • i have an external json file and want to pull this json into the service via $http. The file path is js/products.json.

  • If you want to pull over $http you need to have a server

  • within $http.get cannot place a local file. Example: $http.get("js/products.json")

  • I can’t say for sure, but possibly not. If you have how it will be a great gambiarra, there must be other ways to get this file without http

  • and how this json would look on a server. I have this file hosted on a server. How would you pull it into the Service?

  • For $http. Example: $http.get("http://pokeapi.co/api/v2/pokemon/4");

  • yes.. more as the service structure would look with this $http. I’ve made several attempts but none of them worked.

  • There are several ways to assemble the service structure, I do it this way: http://pastebin.com/3DMuY1FR

  • Note that in $http.get I am using the local ip, where I have a server sending me data in Json format

Show 7 more comments

1 answer

1

You can pull the files that are in the project or on an external server using $http.get, have you tried it this way? I’ll just change your getProducts, I can’t test the full code now, see if it helps you.

getProdutos: function () {
    $http.get('js/produtos.json').then(function (retorno) {
         this.produtos = retorno.data;
         return this.produtos;
    })
}
  • I did the way q vc described me, more error return on the console Uncaught Syntaxerror: Unexpected token ( @André Vicente

  • from a.log(return) console to see what is returning, you may not need the date

  • nothing my...keeps giving the same error...nor the result of the console appeared. I even deleted the date on return.data and still gave the same error @Andrévicente

  • There seems to be a wrong "(" in the code. It may even be in another part of your code. It looks right in the error if it shows the line where the error is. But check everything to see if there’s nothing left or missing there. Take all http.get and test, if you continue the error take the whole service to see if you still have the error, test until you find.

  • the error ta on the line "getProducts: Function(){ @Andrévicente

  • I created this service in a project of mine and I did not have this error. You can play in codepen or edit your code to see how it is now?

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.