0

I know this question has been asked multiple times and most of the answers are same using jQuery or XMLHttp object, etc. But the limitation using those methods is Access-Control-Allow-Origin.

I am looking to check whether a PDF file exists on the server or not before linking to it. If it does not exist I have to show an alert. I have tried XMLHTTP synchronous and asynchronous requests. But I get the below warning in Chrome:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/. http://fiddle.jshell.net/_display/ Failed to load resourceverify @ fiddle.jshell.net/:34

followed by:

Uncaught NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load ...

For the latter, I get

XMLHttpRequest cannot load http://google.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access.

I want to know if it is even possible using some workaround in Pure JS. I cannot use any other JavaScript libraries.

inquizitive
  • 622
  • 4
  • 21
  • possible duplicate of [using javascript to detect whether the url exists before display in iframe](http://stackoverflow.com/questions/10926880/using-javascript-to-detect-whether-the-url-exists-before-display-in-iframe) – JJJ Jul 02 '15 at 12:38
  • 3
    You can't access content on different domains unless the source domain has appropriate access headers. That's what the answers you've seen about "Access-Control-Allow-Origin" mean. Without that, your browser won't load the content. You can check the content from your server-side code however. – Pointy Jul 02 '15 at 12:39
  • This is one of those things that really ought to be done on the server, not in the user's browser, especially for efficiency reasons. – Michael Aaron Safyan Jul 02 '15 at 12:39
  • you could (and it would be even recommended) create server side api that will return this information – Mladen Oršolić Jul 02 '15 at 12:41

0 Answers0