问题
In my polymer app, pulling in json data via iron-ajax element works fine, but I can't get it to work with my local json file. I've hosted the app on firebase, but still no luck.
This doesn't work:
<iron-ajax
auto
url="data/watchlist.json"
last-response="{{data}}"
handle-as="json">
</iron-ajax>
But, this does works:
<iron-ajax
auto
url="http://jsonplaceholder.typicode.com/albums/"
last-response="{{data}}"
handle-as="json">
</iron-ajax>
What am I doing wrong? Can't tell if I'm doing something wrong with polymer or with ajax/json calls.
回答1:
That is probably a browser limitation for security purposes.
Check the browser console you probably get an error that tells you why Chrome is blocking the request.
Start up a server that serves this file and fetch it from there. You need to enable CORS support on the server if it is a different one than where you load index.html
from.
来源:https://stackoverflow.com/questions/35857552/iron-ajax-not-working-with-local-json-file