iron-ajax not working with local json file? - polymer

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.

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.

Related

I get a Cross-Origin Request blocked when trying to use pdf.js

I try to use pdf.js to read a local pdf file.
I added this script tag:
<script src="https://cdn.jsdelivr.net/npm/pdfjs-dist#2.8.335/build/pdf.min.js"> </script>
and I got this simple console.log to see if it works:
<script>pdfjsLib.getDocument('livret-Krismenn.pdf').then(doc => {
console.og('this file has ' + doc._pdfInfo.numPages + 'pages')
}) </script>
but it doesn't.
The reason given by my browser (firefox): The CORS request doesn't use http.
I looked it up and found solutions talking about:
Access-Control-Allow-Credentials: true
but I have no clue what to do with that...
How can I fix this?
The problem is you can't read file from local system directly due to security issues. You need to run a simple web server to load pdf file form. This option you mentioned should be set on webserver side not your side. Try running simple static webserver available with npm,...
You can also use FileReader api to read files on system.

Swagger UI Not Loading Sometimes

I'm using latest version of Swagger in my ASPNetCore 3.1 project and debugging on latest version of Chrome. When I try to enter swagger page it sometimes not loading and i see an empty page. It looks like totally random. It solves after I refresh the page.
I saw these errors on console.
GET https://localhost:44389/swagger/swagger-ui-bundle.js net::ERR_HTTP2_PROTOCOL_ERROR
index.html:95 Uncaught ReferenceError: SwaggerUIBundle is not defined
at window.onload (index.html:95)
File in the error is random. Its sometimes a js file, sometimes a .css file. It changes.
If I publish this project on IIS and disable Http/2 support I never encounter this error.
Why?
You can try the following:
1.Check if all your controller methods have [http] tag. If they all do and still doesn't work go to step 2
2.In your configure function to ensure that you have app.UseStaticFiles(); If it still doesn't work go to step 3
3.Uninstall and reinstall swagger. If it doesn't work go to step 4 (Core Only)
4.If you are using Core Install Microsoft.AspNetCore.StaticFiles and reference it in your project.
I encountered the same problem, SwaggerUIBundle is not defined, and the problem is not on the web app that I'm developing but on my network. The problem was resolved after I switch to another network.

HTML5 application cache: How to exclude the htm-file from cache where the manifest is defined

We are using HTML5 application cache in our single-page-web-application. The manifest-file is defined in the -tag of the index.htm-file:
<html xxxxxxxxxxxxxxxxxxxxxxxxxxxxx manifest="/xxxxxx/xxxxx.appcache">
The index.htm-file is the only htm-file which is loaded from the webserver because the web-application is single-page-web-application build with angularJS. All the following responses are in JSON.
Now we would like that the index.htm-file is not cached in the HTML5 application cache. But because the manifest-file is defined in the index.htm-file this later file is also implicitly cached.
Does anyone know how this could be done that the index.htm-file is not cached although the manifest is defined in the index.htm-file?
We ran into caching problems with http://dukecon.org. Please check out the discussion here: how to selectively disable cache for spring boot (manifest.appcache).
Maybe the second part of our solution will address your issue too: Setting a no-cache header for the index.html file? This is performed with a ServletFilter: https://github.com/dukecon/dukecon_server/blob/master/impl/src/main/java/org/dukecon/server/impl/CacheManifestFilter.groovy
HTH!

Using Razor with Durandal gives 404

i am trying to use cshtml with Durandal
i followed instructions on
http://bartwullems.blogspot.com/2014/03/durandaljs-enable-razor-views.html
main.js is called properly but when it goes looking for the view i get the error.
View Not Found. Searched for "views/shell" via path "text!views/shell.cshtml".
and in the console window i can see a 404 error
http:// local /App/views/shell.cshtml 404 (Not Found)
route is configured and i copied the web.config from View to App/views and the DurandalViewController is also in place.
my main.js looks like one in the example
just the last line is
app.setRoot('viewmodels/shell');
__________________________________
Structure has
App/viewmodels/shell.js
App/views/shell.cshtml
App/views/web.config
Your issue will be the routing in Asp.net.
Most likely the routing engine will be looking for shell.cshtml in http://localhost/views/shared/shell.cshtml or http://localhost/views/yourControllerName/shell.cshtml by default. You will need to add a route to tell the server side where to find the view.
Hi,
for info I had a similar issue.
Everything was working locally but when I deployed my solution to Azure Web sites I got an error:
Not Found. Searched for "views/shell" via path "text!views/shell.cshtml".
In my case it was http error 500.
Solution was to rename a file from "shell" to something else. I renamed it to base.cshtml (and base.js). It looks that somehow shell.cshtml could not be processed on Azure.

How can I load a local json file?

Is there a way to configure firefox using about:config to allow local file access (for demo purposes)?
Specifically using FF12, I need to be able to have local file access to json data. It runs fine on a server, but I'd like to make this demo more portable.
$.ajax({
url: "../_assets/levelschema.json",
complete: function (data) {
Levels = data.levels;
//...
},
success: function (data) {
// wont get called b/c files don't have 200 HTTP status
},
async: false
});
I've tried setting it to not async=false, but I get a "Access to restricted URI denied" error. This is a security feature. I really need the demo to work offline, without internet access, and I'd prefer the people using it not have to install a web server. I'd also prefer not having to embed all my data in the HTML tags since that data subject to change.
You can flip the security.fileuri.strict_origin_policy preference in about:config. But that opens up some security holes; moving your JSON to be in a child directory of the directory the HTML is in is a much better idea.
If the JSON file is on your local computer, access it like this:
<script type="text/javascript" src="myFile.json"></script>
You'll have to add that line to your offline project and remove it when it's live, but it's the best way to do it without any server-side assistance. You'll also have to remove your AJAX code for your demo, because it's not going to work on your local computer.
Ok, JSON requests using AJAX work just fine as long as your data files are down path from your originating HTML file. So I was trying to go up a level to _assets/ directory. The solution is to move _assets into the HTML current directory for access.
You can use AJAX with async=false in this case.