admin管理员组文章数量:1318564
Hi I am trying to use ONLY JavaScript
and HTML
to read the json
object from a URL. I am using the following code:
function getJSONP(url, success) {
var ud = '_' + +new Date,
script = document.createElement('script'),
head = document.getElementsByTagName('head')[0]
|| document.documentElement;
window[ud] = function(data) {
head.removeChild(script);
success && success(data);
};
script.src = url.replace('callback=?', 'callback=' + ud);
head.appendChild(script);
}
getJSONP('http://webURl?&callback=?', function(data){
console.log(data);
});
As you would have guessed I am getting Not at same origin as the document, and parent of track element does not have a 'crossorigin' attribute. Origin 'null' is therefore not allowed access.
FYI the server returns JSON data and doesnot have callback function.
Cheers for your help.
Hi I am trying to use ONLY JavaScript
and HTML
to read the json
object from a URL. I am using the following code:
function getJSONP(url, success) {
var ud = '_' + +new Date,
script = document.createElement('script'),
head = document.getElementsByTagName('head')[0]
|| document.documentElement;
window[ud] = function(data) {
head.removeChild(script);
success && success(data);
};
script.src = url.replace('callback=?', 'callback=' + ud);
head.appendChild(script);
}
getJSONP('http://webURl?&callback=?', function(data){
console.log(data);
});
As you would have guessed I am getting Not at same origin as the document, and parent of track element does not have a 'crossorigin' attribute. Origin 'null' is therefore not allowed access.
FYI the server returns JSON data and doesnot have callback function.
Cheers for your help.
Share Improve this question edited Dec 20, 2016 at 4:56 usrNotFound asked Aug 31, 2015 at 0:29 usrNotFoundusrNotFound 2,8304 gold badges28 silver badges43 bronze badges 6- 1 If the server does not support JSONP and does not support CORS than you are out of luck. JavaScript can not work around the same origin policy. It is there for a reason. – epascarello Commented Aug 31, 2015 at 0:35
- @epascarello so you think If the server support JSONP then this code will works fine. – usrNotFound Commented Aug 31, 2015 at 0:40
- It should work for JSONP – epascarello Commented Aug 31, 2015 at 13:07
- Do you have a mockup of what looks like the actual result of your request? – romuleald Commented Sep 8, 2015 at 11:52
- @romuleald its JSON object. – usrNotFound Commented Sep 8, 2015 at 23:08
2 Answers
Reset to default 7 +50The server either needs to have CORS enabled using headers like this: (Credits to the answer here: CORS with php headers)
// Allow from any origin
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
Or the server needs to output JSONP like:
echo $_GET['callback'] . '(' . json_encode($whatever) . ')';
Another option if this is not on your own server is to create a PHP file on your own server that does a filegetcontents
on the url you need to read (with the JSON data without cors) and echo the same data in JSONP format.
You can then use this new PHP file (url) in your pure javascript getJSON
function.
Without a server in the middle or cors or jsonp, it is not possible.
If you want a quick & working fix, you can fetch the content via an iframe, or use a proxy like YQL.
But I would remend using a backend strategy to fetch your content, then process it with javascript.
本文标签: Javascript Cross domain JSONStack Overflow
版权声明:本文标题:Javascript Cross domain JSON - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742049717a2418002.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论