Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
695 views
in Technique[技术] by (71.8m points)

javascript - Getting Cross Origin Block Request [CORS] error when using .getJSON to get Play Store App Details

So basically i was using 42matters.com APP market API to get the app details or information from the google play store, everything was working fine and i was getting the JSON response back, but when i came back to office after weekend holidays, this rather strange error came and nothing was being returned.

I have used $.getJSON function like:

var packageID = 'com.whatsapp';
   $.getJSON('https://42matters.com/api/1/apps/lookup.json?p='+packageID+'&access_token=accesstoken1234')
.done(function(appDetails) {
            $('#logo').html(JSON.stringify(appDetails));
        });

As mentioned this was returning the data and i was able to change everything accordingly but now it is giving me this error for no reason at all

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://42matters.com/api/1/apps/lookup.json?p=com.whatsapp&access_token=accesstoken1234. This can be fixed by moving the resource to the same domain or enabling CORS

i have enabled the CORS using PHP, and nothing happended, tried to change the conf file of apache to enable CORS there but the service wont restart with this, so i am stuck.

One more thing, when i manually enter the above link in the browser, it does give the desired result. Please help me resolve it

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

CORS is blocked in browser side that won't allow AJAX to request data from another domain. You should contact the 42matters.com team to open the crossdomain.xml for you like this CORS jQuery AJAX request or Can someone post a well formed crossdomain.xml sample?

Or the workaround for AJAX Cross-Domain is JSONP: jQuery AJAX cross domain but the API should support this kind of request.

The last thing is don't use AJAX to collect data from another sources, if it is mobile platform, it should have native HTTP Request object to do this


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...