[Latest version of JavaScript Library]
In my web application, all resources and traffic exclusively use HTTPS protocol.
However, I encountered an issue where I am unable to send requests using $.ajax()
or other related methods like $.get
, $.post
, etc. This problem was flagged by Chrome with the following error message:
Refused to connect to '' due to a Content Security Policy directive violation: "connect-src 'self'".
The root cause was that the AJAX requests were being made over HTTP from an HTTPS page, with no apparent way to enforce HTTPS.
To my surprise, the issue was resolved in an unexpected manner. Attempting to execute $.get('/api/mycall/')
resulted in an error stating "Refused to connect to ''", without showing the ending forward slash as part of the actual code call. It seemed like the error was caused by the absence of the forward slash.
I conducted numerous tests, each confirming that requests fail when there is a trailing slash in the URL being accessed, while they succeed without it.
Interestingly, calling $.ajax({ url: '/api/mycall'})
functions correctly, whereas $.ajax({ url: '/api/mycall/'})
does not.