簡體   English   中英

AngularJS-屬性文件的$ http.get始終返回404

[英]Angularjs - $http.get of properties file always returns 404

也許我在這里做錯了,因為我無法完成這項工作。 我錯過了什么嗎?

services.js

$http({
    method : 'GET',
    url : 'angular.properties'
}).success(function (data, status, headers, config) {
    console.log("good");
}).error(function (data, status, headers, config) {
    console.log("error: " + data); // It's printing this.
});

angular.properties

{ 
    "url": "http://localhost", 
    "port": "",
    "default_language": "es" 
}

應用程式結構

應用程式結構

錯誤

在此處輸入圖片說明

Gruntfile.js

'use strict';

module.exports = function (grunt) {

  grunt.initConfig({
    connect: { 
      server: {
        options: {
          port: 9000,
          base: 'app/'
        }
      }
    },
    watch: { 
      project: {
        files: ['app/**/*.js', 'app/**/*.html', 'app/**/*.json', 'app/**/*.css'],
        options: {
          livereload: true
        }
      }
    },
  });

  grunt.loadNpmTasks('grunt-contrib-connect');
  grunt.loadNpmTasks('grunt-contrib-watch');

  grunt.registerTask('default', ['connect', 'watch']);

};

回答

文件夾“ app”的內容是我應該放在服務器上的內容。 所以我只需要應用程序的內容,而我的angular.properties不在此文件夾中。 那是錯誤。 現在正在工作:)正確的結構是:

在此處輸入圖片說明

我假設您的main.js是您的服務,所以您實際上是從/module而不是/app啟動站點,這意味着您的.properties文件位於網站根目錄下的目錄中。

如果將angular.properties文件移動到module目錄中,則原始代碼應該可以使用。

我建議將angular.properties定義為一個值。 您也可以將其聲明為常量,但是只能將其注入到控制器和服務中。

angular.module(NAME).value('properties', { 
    "url": "http://localhost", 
    "port": "",
    "default_language": "es" 
});

這樣,您可以輕松地將其注入服務中,然后在通話中使用它

$http({
    method : 'GET',
    url : properties.url
}).success(function (data, status, headers, config) {
    console.log("good");
}).error(function (data, status, headers, config) {
    console.log("error: " + data); // It's printing this.
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM