JSONファイルを別のサーバーからロードする必要があります(私は制御できません)。数時間の研究の結果、JSONPは私の嘆願に答える汚れたハックだということが分かった。jsonpとangularjsの使用(CORSできない)
私はlocalhost:3000でgulpを使用してサイトを提供しているnodejsを使用しています。 localhostからJSONをロードする:8000
URLで作業していなかったコードと同じコードを使用して、不要なURL(インターネット上でランダムに見つけたもの)からサンプルを取得できました。
私はそれがそうでないもののようなファイルを読み込もうとしているのだろうかと思います。私が研究できる限り、JSONをJSONPに解析する必要はありません。正しい軌道にいるのですか?
下には、私が話していたコード:
(function() {
'use strict';
angular
.module('qwe.asd')
.controller('UsersController', UsersController);
/** @ngInject */
function UsersController($http, $log, $sce) {
var vm = this;
var trustedUsersAPI = "http://localhost:8000/users?callback=JSON_CALLBACK";
$sce.trustAsResourceUrl(trustedUsersAPI);
$http.jsonp(trustedUsersAPI, {
'callback': 'JSON_CALLBACK'
})
.success(function (data) {
$log.log("request 1 - OK");
$log.log(data);
vm.users = data;
})
.error(function() {
$log.log("request 1 - KO");
});
var url = "http://public-api.wordpress.com/rest/v1/sites/wtmpeachtest.wordpress.com/posts?callback=JSON_CALLBACK";
$sce.trustAsResourceUrl(url);
$http.jsonp(url)
.success(function (data) {
$log.log("request 2 - OK");
$log.log(data);
})
.error(function() {
$log.log("request 2 - KO");
});
}
})();
そして、生成されたログ...
angular.js:13424 request 1 - KO
angular.js:13424 request 2 - OK
angular.js:13424 Object {found: 12, posts: Array[12]}
そして最後に、JSONファイル私は(MIS)読み:
私が必要とするものだが、私は持っていない。
[{"id":0,"firstname":"front","lastname":"end","email":"[email protected]"},{"id":1,"firstname":"back","lastname":"end","email":"[email protected]"},{"id":2,"firstname":"john","lastname":"doe","email":"[email protected]"},{"id":3,"firstname":"dev","lastname":"eloper","email":"[email protected]"},{"id":4,"firstname":"ad","lastname":"min","email":"[email protected]"}]
そして、私は必要がありますが、細かい
{"found":12,"posts":[{"ID":XX,"site_ID":XXXX,"author":XXXX(..POSTS DATA WITH NO VALUE HERE..)}]}
P.S.に動作していない1私はここで本当にグリーンです、私は今朝から角張っています!
so ...動作していて動作していませんか? –
* "JSONPがJSONPをサポートしている場合にのみ、JSONPが私の賛成に答える汚いハックだということが数時間調査された。サーバーがJSON対JSONPのために送り返すものは** different **です。 –
trustedUsersAPIに含まれるJSONは、(Chromeのdevtoolsの)[ネットワーク]タブからアクセスできますが、何らかの形で(たとえば、コードを通じて到達できません。 @ KevinB –