<script>
$.getJSON('url', function (data) {
console.log("Before:"+data);
t = data;
console.log("After:"+t);
});
</script>
When I am using getJson
method to get data from REST API , I am getting the error:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at the url. This can be fixed by moving the resource to the same domain or enabling CORS.
How do I solve this? Please help.
<script>
$.getJSON('url', function (data) {
console.log("Before:"+data);
t = data;
console.log("After:"+t);
});
</script>
When I am using getJson
method to get data from REST API , I am getting the error:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at the url. This can be fixed by moving the resource to the same domain or enabling CORS.
How do I solve this? Please help.
Share Improve this question edited Jul 31, 2014 at 6:03 Phil 165k25 gold badges262 silver badges267 bronze badges asked Jul 31, 2014 at 5:59 user2806784user2806784 251 gold badge2 silver badges4 bronze badges 8- 1 "by moving the resource to the same domain or enabling CORS" or, if the service supports jsonp, using jsonp. – Kevin B Commented Jul 31, 2014 at 6:00
- 1 What it means moving the resource to same domain or how to enable CORS ? – user2806784 Commented Jul 31, 2014 at 6:01
- it means exactly what that text says... i'm not sure how to put it simpler. Move your service (the resource) to the same domain (your webserver) or enable CORS (which is a process in which a cross origin request is made. research it.) – Kevin B Commented Jul 31, 2014 at 6:02
- This question appears to be off-topic because the answer is provided by the error message – Phil Commented Jul 31, 2014 at 6:02
- 1 yes answer is provided. if u can explain what it means. – user2806784 Commented Jul 31, 2014 at 6:03
2 Answers
Reset to default 3Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at the url. This can be fixed by moving the resource to the same domain or enabling CORS.
It means you should have api (url
in your code) and the file which has your script must be in same domain
Or
Add the Access-Control-Allow-Origin header
in the API(url
in your code) domain
<FilesMatch "\.(php)$">
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
</FilesMatch>
*
to allow all cross domainrequests
<FilesMatch "\.(php)$">
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
</FilesMatch>
I added this to my httpd-vhosts.conf and the error was solved