Sal P. Sal P. - 1 year ago 82
Ajax Question

Ajax call to API not working

I am trying to retrieve data from an API using Jquery's ajax(), but it doesn't work with this implemenation:

$.ajax('').done(function(data) {

<script src=""></script>

After running the code the alert function doesn't work, so I concluded that the success callback function isn't working, but I have no idea why.

Answer Source

After inspecting the server response headers, there is no Access-Control-Allow-Origin, this means that the server doesn't allow cross-origin access. Since you make a cross-origin HTTP request, your request will be rejected by the browser following the Same-origin policy:

The same-origin policy restricts how a document or script loaded from one origin can interact with a resource from another origin. It is a critical security mechanism for isolating potentially malicious documents.

Look at your console you will see the following error (Chrome):

XMLHttpRequest cannot load No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '' is therefore not allowed access.

For more details please refer to:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download