I am new to AngularJS. I am using rails application to display data in json format. Data should be used by angular application. Repo angular repo and repo repo are completely different. The reason for different repositories is that I want my rails to simply display data using an API that I can use in an angular application.
My rail controller as below
class Api::V1::PhonesController < ApplicationController def index render json: Phone.all end def show render json: Phone.find(params[:id]) end ... end
Now that I am "localhost: 3000 / api / v1 / phones", it returns me the json data for all phones. When I visit "localhost: 3000 / api / v1 / phones / 1", it returns json data for the phone with id 1. I confirmed the json data format using http://jsonlint.com/ . Everything is working fine so far.
My angularjs route file looks like this:
$routeProvider. when('/phones', { templateUrl: 'list.html', controller: 'PhoneListController' }). when('/phones/:id', { templateUrl: 'show.html', controller: 'PhoneShowController' }). otherwise({ redirectTo: '/phones' }); }]);
My index.html in the angular repository has a list.html template built into it.
<html ng-app='phoneCatApp'> ... </html> <script type="text/ng-template" id="list.html"> This is the list template. </script>
The code for services.js looks like this:
var appServices = angular.module('appServices', []); phoneCatApp.factory('appServices', ['$http', '$q', function($http, $q){ var url = "http://localhost:3000/api/v1/"; //get all phones this.getPhones = function(){ var defered = $q.defer(); var listApi = url + "phones"; $http.jsonp(listApi).then(function(results){ defered.resolve(results); }, function(error){ defered.reject(error); }); return defered.promise; } return this; }]);
The text in the script template is also displayed when I am "# / phones". The problem is that 1) In chrome, when checking a page, the following error is displayed.
Refused to execute script from 'http://localhost:3000/api/v1/phones' because its MIME type ('application/json') is not executable, and strict MIME type checking is enabled.
2) in firefox, the following error is displayed.
SyntaxError: missing ; before statement
Thanks for the help.