admin管理员组文章数量:1026398
Is it possible to download and parse a plain text file from different domain with JavaScript?
I've got this fiddle so far, but I'm stuck figuring out what I'm doing wrong.
Markup:
<div id="clickme">Click me</div>
<div id="result">Result: </div>
Code:
$("#clickme").click(function() {
/* ###################################
NOTE: im on say example/test.html but trying
NOTE: to access different_domain_sample
*/
var req = new XMLHttpRequest();
var sURL = ".txt";
req.open("GET", sURL, true);
req.setRequestHeader("User-Agent", "blah/4.2");
req.onreadystatechange = function() {
if (req.readyState == 4) {
$("#result").text("Result is: <pre>" + req.responseText + "</pre>");
}
};
req.send(null);
});
Already answered, but more info on this is here Cross-origin resource sharing
Is it possible to download and parse a plain text file from different domain with JavaScript?
I've got this fiddle so far, but I'm stuck figuring out what I'm doing wrong.
Markup:
<div id="clickme">Click me</div>
<div id="result">Result: </div>
Code:
$("#clickme").click(function() {
/* ###################################
NOTE: im on say example./test.html but trying
NOTE: to access different_domain_sample.
*/
var req = new XMLHttpRequest();
var sURL = "http://www.google./robots.txt";
req.open("GET", sURL, true);
req.setRequestHeader("User-Agent", "blah/4.2");
req.onreadystatechange = function() {
if (req.readyState == 4) {
$("#result").text("Result is: <pre>" + req.responseText + "</pre>");
}
};
req.send(null);
});
Already answered, but more info on this is here Cross-origin resource sharing
Share Improve this question edited Apr 21, 2012 at 8:52 lzdt asked Apr 21, 2012 at 8:30 lzdtlzdt 4991 gold badge6 silver badges18 bronze badges 3- 2 You can't send requests across domains via JavaScript. – Blender Commented Apr 21, 2012 at 8:35
- @Blender Is this same limitation as in java's applets? Can I "sign" the code or ask for user whether he trusts the code, so its possible to access files across domains or something like that? – lzdt Commented Apr 21, 2012 at 8:37
- @lzdt: Yes, that would be possible. Or do it like ninja suggests, by hosting a proxy service on the same domain. – Niklas B. Commented Apr 21, 2012 at 8:37
2 Answers
Reset to default 4It's because of browsers have implemented a feature called cross-site-scripting-prevention. You could for example do the ajax request on a php file on the same server and in that query the target page using curl.
There are three ways to do this:
- With the help of a non-browser-based proxy served from your domain which will fetch the data on your behalf. You can also use a plugin which can bypass the same-origin policy.
- Use JSONP or another similarly hackish way around the same-origin policy. This would require the web server to support JSONP.
- Disable the cross-origin policy (definitely not remended; very dangerous)
Is it possible to download and parse a plain text file from different domain with JavaScript?
I've got this fiddle so far, but I'm stuck figuring out what I'm doing wrong.
Markup:
<div id="clickme">Click me</div>
<div id="result">Result: </div>
Code:
$("#clickme").click(function() {
/* ###################################
NOTE: im on say example/test.html but trying
NOTE: to access different_domain_sample
*/
var req = new XMLHttpRequest();
var sURL = ".txt";
req.open("GET", sURL, true);
req.setRequestHeader("User-Agent", "blah/4.2");
req.onreadystatechange = function() {
if (req.readyState == 4) {
$("#result").text("Result is: <pre>" + req.responseText + "</pre>");
}
};
req.send(null);
});
Already answered, but more info on this is here Cross-origin resource sharing
Is it possible to download and parse a plain text file from different domain with JavaScript?
I've got this fiddle so far, but I'm stuck figuring out what I'm doing wrong.
Markup:
<div id="clickme">Click me</div>
<div id="result">Result: </div>
Code:
$("#clickme").click(function() {
/* ###################################
NOTE: im on say example./test.html but trying
NOTE: to access different_domain_sample.
*/
var req = new XMLHttpRequest();
var sURL = "http://www.google./robots.txt";
req.open("GET", sURL, true);
req.setRequestHeader("User-Agent", "blah/4.2");
req.onreadystatechange = function() {
if (req.readyState == 4) {
$("#result").text("Result is: <pre>" + req.responseText + "</pre>");
}
};
req.send(null);
});
Already answered, but more info on this is here Cross-origin resource sharing
Share Improve this question edited Apr 21, 2012 at 8:52 lzdt asked Apr 21, 2012 at 8:30 lzdtlzdt 4991 gold badge6 silver badges18 bronze badges 3- 2 You can't send requests across domains via JavaScript. – Blender Commented Apr 21, 2012 at 8:35
- @Blender Is this same limitation as in java's applets? Can I "sign" the code or ask for user whether he trusts the code, so its possible to access files across domains or something like that? – lzdt Commented Apr 21, 2012 at 8:37
- @lzdt: Yes, that would be possible. Or do it like ninja suggests, by hosting a proxy service on the same domain. – Niklas B. Commented Apr 21, 2012 at 8:37
2 Answers
Reset to default 4It's because of browsers have implemented a feature called cross-site-scripting-prevention. You could for example do the ajax request on a php file on the same server and in that query the target page using curl.
There are three ways to do this:
- With the help of a non-browser-based proxy served from your domain which will fetch the data on your behalf. You can also use a plugin which can bypass the same-origin policy.
- Use JSONP or another similarly hackish way around the same-origin policy. This would require the web server to support JSONP.
- Disable the cross-origin policy (definitely not remended; very dangerous)
本文标签: javascriptIs it possible to load and parse file on remote serverStack Overflow
版权声明:本文标题:javascript - Is it possible to load and parse file on remote server? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745641069a2160750.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论