Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #8628
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Arno Welzel <usenet@arnowelzel.de> |
| Newsgroups | comp.lang.javascript |
| Subject | Re: Read url into a string? |
| Date | Fri, 25 Nov 2011 15:59:02 +0100 |
| Organization | A noiseless patient Spider |
| Lines | 80 |
| Message-ID | <4ECFAD36.7070800@arnowelzel.de> (permalink) |
| References | <a0faab1d-920c-4d58-bb9f-b382b0b4ce38@w3g2000vbw.googlegroups.com> <pmuqc7t8p6ees0qo6sh484mc2o7jvv4afj@4ax.com> <jajvki$fvb$1@sunce.iskon.hr> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | 8bit |
| Injection-Info | mx04.eternal-september.org; posting-host="dd4uQGf4fHOQcq7/hg1u1Q"; logging-data="11850"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/jgklJ44RFjbpkmu7UMs+tT3FO/QsvOx4=" |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0 |
| In-Reply-To | <jajvki$fvb$1@sunce.iskon.hr> |
| Cancel-Lock | sha1:alzyjVLOeM29WxR8PTvHiDzcgV8= |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.javascript:8628 |
Show key headers only | View raw
Zlatko Đurić, 2011-11-24 00:29:
> On 11/24/2011 12:00 AM, Gene Wirchenko wrote:
>
>> JavaScript is not Java. Try posting to a Java newsgroup.
>
> Alternatively, the OP can try with JavaScript.
>
> Maybe:
>
> function getJSONfromUrl(url) {
> if (window.XMLHttpRequest) {
> xhr=new XMLHttpRequest();
> } else {
> xhr=new ActiveXObject("Microsoft.XMLHTTP");
> }
> xhr.open("GET",url,false);
> xhr.send();
> return xhr.responseText;
> }
A more complete approach:
function getJSONfromUrl(url)
{
var request=null;
var response="";
try
{
request = new XMLHttpRequest();
}
catch(ms)
{
try
{
request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(nonms)
{
try
{
request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (failed)
{
request = null;
}
}
}
if(request != null)
{
request.open("GET", url, false);
request.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
request.send(null);
response = request.responseText;
}
return response;
}
But you have to consider that this is only possible if either the target
url is within the same domain as the script (same origin policy) OR if
the target server provides the appropriate CORS headers and the browser
supports CORS at all (see <http://www.w3.org/TR/cors/>).
If you need to access URLs outside your own domain you have to implement
a proxy on your own server which forwards the requests to the target and
returns the results to the script.
--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Read url into a string? SpreadTooThin <bjobrien62@gmail.com> - 2011-11-23 14:09 -0800
Re: Read url into a string? Gene Wirchenko <genew@ocis.net> - 2011-11-23 15:00 -0800
Re: Read url into a string? SpreadTooThin <bjobrien62@gmail.com> - 2011-11-23 15:28 -0800
Re: Read url into a string? Zlatko Đurić <zladuric@gmail.com> - 2011-11-24 00:29 +0100
Re: Read url into a string? SpreadTooThin <bjobrien62@gmail.com> - 2011-11-23 15:34 -0800
Re: Read url into a string? SpreadTooThin <bjobrien62@gmail.com> - 2011-11-23 15:37 -0800
Re: Read url into a string? SpreadTooThin <bjobrien62@gmail.com> - 2011-11-23 16:35 -0800
Re: Read url into a string? Scott Sauyet <scott.sauyet@gmail.com> - 2011-11-23 18:02 -0800
Re: Read url into a string? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-24 21:27 +0100
Re: Read url into a string? Zlatko Đurić <zladuric@gmail.com> - 2011-11-24 22:13 +0100
Re: Read url into a string? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-24 22:31 +0100
Re: Read url into a string? Zlatko Đurić <zladuric@gmail.com> - 2011-11-24 23:08 +0100
Re: Read url into a string? Arno Welzel <usenet@arnowelzel.de> - 2011-11-25 15:59 +0100
Re: Read url into a string? "Evertjan." <test@abc.invalid> - 2011-11-26 13:44 -0400
csiph-web