javascript - Angular JSON 仅适用于本地 json 文件,不适用于 url

标签 javascript json angularjs

我刚刚接触 Angularjs,并尝试使用 $http.get。仅当我提供的 url 是本地“calender.json”时,该页面才有效,但是当我提供 url 时,它会显示一个空白页面。有人可以帮助我理解我的错误吗?

最佳答案

正如其他人在对您的问题的评论中指出的那样,这是一个 CORS 问题。要轻松解决此问题,您可以将 $http.get() 调用更改为 $http.jsonp()。然而,仅靠这一点并不能解决问题。您还需要通过添加另一个查询参数来调整 url,如下所示(使用您提供的 url): https://ssl.uh.edu/calendar/api/?view=7day&format=json&distinct=1&callback=JSON_CALLBACK

callback=JSON_CALLBACK 告诉服务器您正在查询,您希望它将 JSON 响应包装在名为 JSON_CALLBACK 的函数中,这是一个特殊的 Angular 关键字。查看文档以获取更多信息:https://docs.angularjs.org/api/ng/service/ $http

tl;博士版本: 将 $http.get("https://ssl.uh.edu/calendar/api/?view=7day&format=json&distinct=1") 更改为 $http.jsonp("https://ssl.uh.edu/calendar/api/?view=7day&format=json&distinct=1&callback=JSON_CALLBACK")

关于javascript - Angular JSON 仅适用于本地 json 文件,不适用于 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31441168/

相关文章:

javascript - 如何替换 html 或 ajax 中的返回值

c# - 想要清除文本框+焦点文本框+防止同一按钮上的asp.net回发

javascript - 使用 JSON 数据填充 HTML

javascript - 从 JSON 中填充两个选择

javascript - 在 Archetype - Umbraco 7 中获取 Vorto 字段的标签模板值

Javascript原型(prototype)继承基函数未定义

javascript - Knex Heroku 错误 : self signed certificate

javascript - AngularJS PDF 查看器无法在 IE 中运行

javascript - MongoDB 和 Mongoose : How to retrieve nested Object

javascript - 带开关的 Angular 过滤器列表( Angular Material )?