0
我運行一個應用程序express.js在那裏我有一個返回JSON對象錯誤:無法設置頭它們在express.js應用程序發送
https://serene-depths-3284.herokuapp.com/chapters?setLng=en-UK
在我的控制器文件夾的路線後,我有chapters.coffee文件,該文件是如下
# Return the chapters.json based on the language
i18n = require "i18next"
fs = require "fs"
cldr = require "cldr"
__ = require "underscore"
exports.chapters = (req, res, err) ->
fs.readFile "./data/chapters.json", (err, chapterJSON) ->
console.log("read file error", err) if err
tzmChapters = JSON.parse chapterJSON
try
# ...
lngCode = req.query.setLng.split("-")[0]
catch e
# fallback to user locale
lngCode = i18n.lng().split("-")[1]
allCountries = cldr.extractTerritoryDisplayNames(lngCode)
tzmNetwork = []
tzm = __.each tzmChapters, (value, index, list) ->
locale = value.desc.LOCALES.split("-")[1]
tzmNetwork.push({link:value.desc.WEBSITE,contact:value.desc.CONTACT,country: allCountries[locale]})
res.json {tzmNetwork}
的代碼可以在https://github.com/TZM/tzm-blade/blob/master/app/controllers/chapters.coffee
發現,爲什麼我嘗試訪問ŧ他從不同的網站的章節JSON文件,我得到:
XMLHttpRequest cannot load http://localhost:3000/chapters. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access.
我如何在我的代碼實現這一點,有沒有更好的辦法,因爲我也在footer.blade文件,我用它來做到這一點請閱讀我的apps.coffee文件中的chapters.json文件https://github.com/TZM/tzm-blade/blob/master/app/config/apps.coffee#L163
任何建議是非常感謝。
看到這個答案http://stackoverflow.com/a/16046774/1221252 –