Check if the resource exists using JS without getting 404 | Community
Skip to main content
New Participant
January 16, 2023
Solved

Check if the resource exists using JS without getting 404

  • January 16, 2023
  • 3 replies
  • 2395 views

Hi All,

 

Is there a way to check if a resource exists using JS without getting 404?

I am unable to find any library which does this job. I am using clientside JS.

For example : I am using $.getJSON() but it does throw 404 in console logs, if its an invalid resource

 

Kindly let me know the best solution for the above issue.

Thanks in advance!

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Monendra_Singh

Hi,

How about below 2 options?

  • One option is to use the HEAD method instead of GET method in your jQuery's ajax method. The HEAD method will only retrieve the headers of the response, so it doesn't download the entire resource.
  • You can also use the XMLHttpRequest object to make a HEAD request and check the status property of the XMLHttpRequest object.
var xhr = new XMLHttpRequest(); xhr.open('HEAD', 'https://example.com/resource'); xhr.onreadystatechange = function() { if (xhr.readyState === 4) { if (xhr.status === 200) { console.log('Resource exists'); } else if (xhr.status === 404) { console.log('Resource does not exist'); } } }; xhr.send();

 

Thanks,

Monendra

 

3 replies

BrianKasingli
New Participant
January 18, 2023

Typically AEM public websites has the /content/my-site/home/jcr:content/root/container/componentA path blocked, because of security reasons by the dispatcher. The reason why you might be getting 404 is because during development, your session is set as admin:admin, while testing in dispatcher, the rules has restricted you to access such resource path from the JCR in AEM publisher.

Another option is, you can write a custom servlet that accepts a parameter. This parameter can be ?path for example. When this servlet is enabled, you can make your JS call your own servlet, and you can return JSON content-type,  and 200 server status, and the text { status: true }. But you might want to watch out for brute force attacks where calling your servlet is going to spike the load of your AEM publishers.

Monendra_Singh
Monendra_SinghAccepted solution
New Participant
January 17, 2023

Hi,

How about below 2 options?

  • One option is to use the HEAD method instead of GET method in your jQuery's ajax method. The HEAD method will only retrieve the headers of the response, so it doesn't download the entire resource.
  • You can also use the XMLHttpRequest object to make a HEAD request and check the status property of the XMLHttpRequest object.
var xhr = new XMLHttpRequest(); xhr.open('HEAD', 'https://example.com/resource'); xhr.onreadystatechange = function() { if (xhr.readyState === 4) { if (xhr.status === 200) { console.log('Resource exists'); } else if (xhr.status === 404) { console.log('Resource does not exist'); } } }; xhr.send();

 

Thanks,

Monendra

 

SureshDhulipudi
New Participant
January 16, 2023

Please see this thread

When data is retrieved from remote servers (which is only possible using the script or jsonp data types), the error callbacks and global events will never be fired.

 

https://stackoverflow.com/questions/14370678/jquery-getjson-error-isnt-being-executed-on-404-error/14371049#14371049