标签:javascript angularjs json ajax cors
我正在尝试使用以下代码从Wikia API检索数据.但一切似乎都失败了.据我所知,必须在服务器上启用CORS,我试图从中检索数据.但是我不知道Wikia是否就是这种情况.所以JSONP似乎是唯一的方法.但是我得到了错误
XMLHttpRequest无法加载http://adventuretime.wikia.com/api/v1/Articles/List?expand=1\u0026amp;category=Episodes\u0026amp;limit=200.请求的资源上不存在“Access-Control-Allow-Origin”标头.因此,不允许来源“http://website.com”访问.
我已经阅读了AngularJS文档:https://docs.angularjs.org/api/ng/service/ $http
w3schools:http://www.w3schools.com/angular/angular_http.asp
有关stackoverflow的大量问题.我的代码有什么可怕的错误吗? Angular应该原生支持CORS和JSONP请求
我的代码如下:
的index.html
<!DOCTYPE html>
<html ng-app="episodes">
<head>
<meta charset="UTF-8">
<title>Episodes</title>
<script src="angular.js"></script>
<script src="workshop.js"></script>
</head>
<body ng-controller="AppController as app">
<table>
<thead>
<tr>
<th>Title</th>
<th>Url</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="episode in app.episodes">
<td>{{episode.title}}</td>
<td>{{episode.url}}</td>
</tr>
</tbody>
</table>
</body>
</html>
workshop.js
(function(){
angular
.module('episodes', [])
.controller('AppController', function($http){
var app = this;
app.episodes = $http({
url: "http://adventuretime.wikia.com/api/v1/Articles/List?expand=1&category=Episodes&limit=200",
dataType: "jsonp"
});
});
})();
任何帮助表示赞赏!
在此先感谢您的帮助
编辑
响应标头是:
Content-Encoding: gzip
X-Content-Type-Options: nosniff
X-Cacheable: YES
Age: 0
X-Cache: ORIGIN, MISS, MISS
Content-Length: 21965
X-Served-By: ap-s21, cache-wk-sjc3160-WIKIA, cache-fra1233-FRA
X-Backend-Response-Time: 2.267
Server: Apache
X-Timer: S1439118796.704444,VS0,VE2447
Vary: Accept-Encoding
Content-Type: application/json; charset=utf-8
Cache-Control: public, max-age=86400
Accept-Ranges: bytes
X-Cache-Hits: ORIGIN, 0, 0
解决方法:
尝试使用$http.jsonp(url);用于跨域请求;
Angularjs api – $http.jsonp();
标签:javascript,angularjs,json,ajax,cors 来源: https://codeday.me/bug/20191002/1841378.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。