Question

I' trying to make get JSON data from an API but I go some error. Here is my code :

$.ajax({
    url: 'http://crawler.studio-lol.com/news.php',
        success: function(result) {
            alert('ok');
        }
    });

But I got error. I add allow cross in my php file header but still get nothing. I tried my ajax call by Github API and it works fine here is code :

$.ajax({
    url: 'https://api.github.com/',
        success: function(result) {
            alert('ok');
        }
    });

I confused a lot I check the Github API headers and put them on my header but still nothing happen.

If any one have idea about how should I put headers in my PHP file or how can I solve my problem please let me know.

Tnx

Was it helpful?

Solution

I can access that url in my browser so it obviously doesn't need any special headers. Considering I am seeing debug data at that url, I assume that the backend belongs to you.

In that case, I would imagine you're running into CORS issues if your front-end, where you have this ajax code, is on a different domain (if you are using steroids, you are definitely on a different domain as steroids by default uses http://localhost).

Official information regarding CORS is here

Access-Control-Allow-Origin: * should resolve your problem.

Note that you would need to set response headers at your backend, not the ajax request.

Hope that helps, let me know if you need me to expand on that or if I am mistaken with my assumptions.

This is also a good guide on the topic

OTHER TIPS

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top