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.

4
  • possible duplicate of using javascript to detect whether the url exists before display in iframe Commented Jul 2, 2015 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. Commented Jul 2, 2015 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. Commented Jul 2, 2015 at 12:39
  • you could (and it would be even recommended) create server side api that will return this information Commented Jul 2, 2015 at 12:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.