LoginSignup
2
2

More than 3 years have passed since last update.

【Vue.js】Vue.js サンプル「商品データ一覧」axiosでAPIリクエスト(jsonファイル読み込み)

Last updated at Posted at 2019-08-30

↓↓完成動画
https://twitter.com/nonnonkapibara/status/1167474872043180032

↓↓実際に動かせる
https://noteitemlist.netlify.com/



商品データ一覧 jsonファイル

nonItemList.json

[
      {"no": 1, "product_name":"coffee", "imgPath":"https://noteitemlist.netlify.com/pic/coffee.png", "amount":800, "shipping_fee":150, "model_number":110, "zooFlag":false},
      {"no": 2, "product_name":"deer", "imgPath":"https://noteitemlist.netlify.com/pic/deer.png", "amount":100, "shipping_fee":0, "model_number":106, "zooFlag":true},
      {"no": 3, "product_name":"duck", "imgPath":"https://noteitemlist.netlify.com/pic/duck.png", "amount":300, "shipping_fee":0, "model_number":101, "zooFlag":true},
      {"no": 4, "product_name":"fox", "imgPath":"https://noteitemlist.netlify.com/pic/fox.png", "amount":400, "shipping_fee":120, "model_number":100, "zooFlag":true},
      {"no": 5, "product_name":"goldfish", "imgPath":"https://noteitemlist.netlify.com/pic/goldfish.png", "amount":900, "shipping_fee":0, "model_number":112, "zooFlag":true},
      {"no": 6, "product_name":"horse", "imgPath":"https://noteitemlist.netlify.com/pic/horse.png", "amount":1100, "shipping_fee":50, "model_number":102, "zooFlag":true},
      {"no": 7, "product_name":"ladybug", "imgPath":"https://noteitemlist.netlify.com/pic/ladybug.png", "amount":1000, "shipping_fee":150, "model_number":111, "zooFlag":true},
      {"no": 8, "product_name":"meat", "imgPath":"https://noteitemlist.netlify.com/pic/meat.png", "amount":200, "shipping_fee":50, "model_number":109, "zooFlag":false},
      {"no": 9, "product_name":"panda", "imgPath":"https://noteitemlist.netlify.com/pic/panda.png", "amount":600, "shipping_fee":80, "model_number":108, "zooFlag":true},
      {"no": 10, "product_name":"penguin", "imgPath":"https://noteitemlist.netlify.com/pic/penguin.png", "amount":1200, "shipping_fee":50, "model_number":103, "zooFlag":true},
      {"no": 11, "product_name":"pig", "imgPath":"https://noteitemlist.netlify.com/pic/pig.png", "amount":500, "shipping_fee":100, "model_number":105, "zooFlag":true},
      {"no": 12, "product_name":"popcorn","imgPath":"https://noteitemlist.netlify.com/pic/popcorn.png", "amount":1300, "shipping_fee":110, "model_number":107, "zooFlag":false},
      {"no": 13, "product_name":"tako", "imgPath":"https://noteitemlist.netlify.com/pic/tako.png", "amount":700, "shipping_fee":90, "model_number":104, "zooFlag":true},
      {"no": 14, "product_name":"post", "imgPath":"https://noteitemlist.netlify.com/pic/post.png", "amount":1400, "shipping_fee":80, "model_number":116, "zooFlag":false},
      {"no": 15, "product_name":"castle", "imgPath":"https://noteitemlist.netlify.com/pic/castle.png", "amount":1500, "shipping_fee":90, "model_number":117, "zooFlag":false},
      {"no": 16, "product_name":"note", "imgPath":"https://noteitemlist.netlify.com/pic/notebook.png", "amount":1700, "shipping_fee":0, "model_number":115, "zooFlag":false},
      {"no": 17, "product_name":"game", "imgPath":"https://noteitemlist.netlify.com/pic/game.png", "amount":1600, "shipping_fee":50, "model_number":114, "zooFlag":false}
    ]

CodePenに実装すると
スクリーンショット 2019-08-31 1.47.02.png
Jsonファイルを読み込む場合、下記エラーとなりました。
異なるドメインからの呼び出しは制限されているようです。

Access to XMLHttpRequest at 'https://noteitemlist.netlify.com/nonItemList.json' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

スクリーンショット 2019-08-31 1.49.44.png

なので、VuejsとJsonファイルのサーバーを同じ場所に置いたら、ちゃんと動きました。






2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2