{"id":960,"date":"2020-11-06T23:25:30","date_gmt":"2020-11-06T14:25:30","guid":{"rendered":"https:\/\/www.typea.info\/blog\/?p=960"},"modified":"2020-11-07T08:52:43","modified_gmt":"2020-11-06T23:52:43","slug":"angluar_other_domain_read_xml","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2020\/11\/06\/angluar_other_domain_read_xml\/","title":{"rendered":"Angular \u306b \u5225\u30c9\u30e1\u30a4\u30f3\u306e\u30d6\u30ed\u30b0RSS\u3092\u8aad\u307f\u8fbc\u307f(CORS\u5bfe\u5fdc\u306a\u3069)"},"content":{"rendered":"<p>\u00a0<\/p>\n<p><a href=\"https:\/\/www.amazon.co.jp\/Angular%E3%82%A2%E3%83%97%E3%83%AA%E3%82%B1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0-%E5%B1%B1%E7%94%B0-%E7%A5%A5%E5%AF%9B\/dp\/4774191302\/ref=as_li_ss_il?_encoding=UTF8&amp;qid=&amp;sr=8-1&amp;linkCode=li2&amp;tag=typea09-22&amp;linkId=258a1e231146b56d46ca22b602034816\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" src=\"\/\/ws-fe.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=4774191302&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=JP&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=typea09-22\" border=\"0\" \/><\/a><img loading=\"lazy\" decoding=\"async\" style=\"border: none !important; margin: 0px !important;\" src=\"https:\/\/ir-jp.amazon-adsystem.com\/e\/ir?t=typea09-22&amp;l=li2&amp;o=9&amp;a=4774191302\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/><\/p>\n<p>\u00a0<\/p>\n<p>Angular \u306e\u30da\u30fc\u30b8\u306b\u3001<a href=\"https:\/\/www.typea.info\/blog\/index.php\/feed\/\">\u30d6\u30ed\u30b0\u306eRSS\u30d5\u30a3\u30fc\u30c9<\/a>\u00a0\u3092\u8aad\u307f\u8fbc\u307f\u305f\u3044\u3068\u3002<\/p>\n<h3>1.<a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/HTTP\/CORS\">\u30aa\u30ea\u30b8\u30f3\u9593\u30ea\u30bd\u30fc\u30b9\u5171\u6709(CORS)<\/a><\/h3>\n<p>\u5358\u7d14\u306b\u3001Angular\u306eHttpClient\u3067\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u6295\u3052\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30a8\u30e9\u30fc\u3002<\/p>\n<blockquote>\n<p>ccess to XMLHttpRequest at &#8216;https:\/\/www.typea.info\/blog\/index.php\/feed\/&#8217; from origin &#8216;http:\/\/localhost:4200&#8217; has been blocked by CORS policy: No &#8216;Access-Control-Allow-Origin&#8217; header is present on the requested resource.<\/p>\n<\/blockquote>\n<p>\u958b\u767a\u4e2d\u306f\u3001\u3059\u3067\u306b\u904b\u7528\u3057\u3066\u3044\u308b\u30d6\u30ed\u30b0\u306b\u3001localhost\u304b\u3089\u30a2\u30af\u30bb\u30b9\u3059\u308b\u305f\u3081\u3001\u5f53\u7136\u30aa\u30ea\u30b8\u30f3\u304c\u7570\u306a\u308b\u305f\u3081\u3001\u5bfe\u5fdc\u3092\u884c\u3046\u3002<\/p>\n<h3>2.\u30b5\u30fc\u30d0\u30fc\u5074\u5bfe\u5fdc<\/h3>\n<p>\u4ee5\u4e0b\u3042\u305f\u308a\u3092\u53c2\u8003\u306b\u3001\u30b5\u30fc\u30d0\u30fc\u5074\u306e\u5bfe\u5fdc<\/p>\n<p>CORS\u306e\u5bfe\u5fdc\u306b\u3064\u3044\u3066\u3001\u30b9\u30c6\u30c3\u30d7\u30d0\u30a4\u30b9\u30c6\u30c3\u30d7\u3067\u89e3\u6c7a\u3057\u3066\u3044\u3063\u3066\u304a\u308a\u3001\u308f\u304b\u308a\u3084\u3059\u3044\u3002<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/qiita.com\/mochizukikotaro\/items\/6b72ad595db8a6b5514f\">https:\/\/qiita.com\/mochizukikotaro\/items\/6b72ad595db8a6b5514f<\/a><\/p>\n<\/li>\n<\/ul>\n<p>\u4ed6\u306b\u306f\u3053\u306e\u8fba\u308a<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/riptutorial.com\/ja\/apache\/example\/19826\/cors%E3%82%92%E6%9C%89%E5%8A%B9%E3%81%AB%E3%81%99%E3%82%8B\">https:\/\/riptutorial.com\/ja\/apache\/example\/19826\/cors%E3%82%92%E6%9C%89%E5%8A%B9%E3%81%AB%E3%81%99%E3%82%8B<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/HTTP\/CORS\">https:\/\/developer.mozilla.org\/ja\/docs\/Web\/HTTP\/CORS<\/a><\/p>\n<\/li>\n<li><a href=\"https:\/\/www.typea.info\/blog\/index.php\/feed\/\">https:\/\/www.typea.info\/blog\/index.php\/feed\/<\/a><\/li>\n<\/ul>\n<p>\u00a0<\/p>\n<p>\u30b5\u30fc\u30d0\u30fc\u5074\u306e\u8a2d\u5b9a (\/etc\/httpd\/conf.d\/xxxx.conf) \u306b\u4ee5\u4e0b\u3092\u8ffd\u8a18\u3057\u3001\u30d6\u30ed\u30b0\u306eURL(<a href=\"https:\/\/www.typea.info\/blog\/index.php\/feed\/\">https:\/\/www.typea.info\/blog\/index.php\/feed\/<\/a>)\u306b\u5bfe\u3057\u3066\u3001\u8a2d\u5b9a\u3092\u884c\u3046<\/p>\n<p>\u00a0<\/p>\n<pre class=\"prettyprint\">&lt;Directory \"\/var\/www\/html\/blog\"&gt;\n    Header Set Access-Control-Allow-Origin \"*\"\n    Header Set Access-Control-Allow-Headers \"Origin, X-Requested-With, Content-Type, Accept\"\n&lt;\/Directory&gt;\n<\/pre>\n<h3>3.Angular\u3067\u6271\u3046\u305f\u3081\u306b\u3001XML\u3092JSON\u306b\u5909\u63db<\/h3>\n<p>\u5909\u63db\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/p>\n<pre class=\"prettyprint\">npm install \u2014save fast-xml-parser<\/pre>\n<p>\u3068\u308a\u3042\u3048\u305a\u3001RSS\u3092\u53d6\u5f97\u3057\u3001\u30b3\u30f3\u30bd\u30fc\u30eb\u306b\u51fa\u529b\u3059\u308b\u3002<\/p>\n<ul>\n<li>responseType : \u2019text\u2019 \u3092\u6307\u5b9a\u3057\u3066\u3001\u30c6\u30ad\u30b9\u30c8\u3068\u3057\u3066\u53d6\u5f97\u3059\u308b\u5fc5\u8981\u3042\u308a<\/li>\n<\/ul>\n<pre class=\"prettyprint\">import { Injectable } from '@angular\/core';\nimport { HttpClient } from '@angular\/common\/http';\nimport * as xmlParser from 'fast-xml-parser';\n\n@Injectable()\nexport class FeedService {\n\n    constructor(private http: HttpClient){}\n    async getFeeds() {\n        try {\n            const url = 'https:\/\/www.typea.info\/blog\/index.php\/feed\/';\n            const res = await this.http.get(\n                url, \n                { responseType : 'text'}\n                ).toPromise();\n                \n            console.log(res);\n            const json = xmlParser.parse(res);\n            console.log(json);\n            return json;    \n        } catch(e) {\n            console.log(e);\n        }\n    }\n}<\/pre>\n<h3>4.\u52d5\u4f5c\u78ba\u8a8d<\/h3>\n<h4>4.1 XML\u304c\u53d6\u5f97\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u3063\u305f<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"rss_xml.png\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2020\/11\/rss_xml.png\" alt=\"RSS\" width=\"599\" height=\"230\" border=\"0\" \/><\/p>\n<h4>4.2 XML\u3092JSON\u306b\u5909\u63dbOK<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"json_rss.png\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2020\/11\/json_rss.png\" alt=\"XML TO JSON\" width=\"598\" height=\"215\" border=\"0\" \/><\/p>\n<p>\u3044\u3058\u3087\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0 \u00a0 Angular \u306e\u30da\u30fc\u30b8\u306b\u3001\u30d6\u30ed\u30b0\u306eRSS\u30d5\u30a3\u30fc\u30c9\u00a0\u3092\u8aad\u307f\u8fbc\u307f\u305f\u3044\u3068\u3002 1.\u30aa\u30ea\u30b8\u30f3\u9593\u30ea\u30bd\u30fc\u30b9\u5171\u6709(CORS) \u5358\u7d14\u306b\u3001Angular\u306eHttpClient\u3067\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u6295\u3052\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30a8\u30e9\u30fc\u3002 ccess [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":962,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[102],"tags":[],"class_list":["post-960","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/960","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=960"}],"version-history":[{"count":3,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/960\/revisions"}],"predecessor-version":[{"id":964,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/960\/revisions\/964"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media\/962"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}