30432

Reading external plain text content into string

Question:

I have a simple CGI script that would generate plain text content on demand. For example, http://1.2.3.4/hello.cgi?name=Joe would return Hello Joe!.

How can I read this into a string in Javascript?

name = "Joe"; url = "http://1.2.3.4/hello.cgi?name=" + name; greeting = loadThis(url);

I'm new to Javascript, so even naive approach (i.e. no need to URL escape...) will be helpful for me :)

Answer1:

Based on this <a href="http://www.javascripter.net/faq/xmlhttpr.htm" rel="nofollow">FAQ on JavaScriper.net</a>, I have found solution that works for me. However, the called script must be on the same machine as the caller, otherwise I get security errors from browsers.

Apparently this is what @Makkes mentioned. However, I'm perfectly happy with having the hello.cgi on the same machine for now.

Here is the code:

function loadThis(localuri) { var oRequest = new XMLHttpRequest(); var sURL = 'http://' + self.location.hostname + localuri; oRequest.open('GET',sURL,false); oRequest.setRequestHeader('User-Agent',navigator.userAgent); oRequest.send(null); if (oRequest.status==200) return(oRequest.responseText); else alert('Error executing XMLHttpRequest call!'); } name = "Joe"; localuri = "/hello.cgi?name=" + name; greeting = loadThis(localuri);

(Of course, this would not handle names with spaces or special characters correctly, but that's another story.)

Recommend

  • How do I copy relevant user information (not all of it) from one production database to another for
  • Weird channel behavior in go
  • Setting GOOGLE_APPLICATION_CREDENTIALS still doesn't enable the access to Google Storage
  • Excel/VBA Rows and column if statement
  • gRPC-java - Compilation of a .proto file
  • How to Output value from function to caller but not to console
  • Returning JSON response from Spring Exception Handler
  • Help troubleshooting System.BadImageFormatException:
  • How do I call a Flex SWF from a remote domain using Flash (AS3)?
  • PHP returning references - '&' necessary in caller?
  • Exclude field from a form in JSF
  • Why does pattern matching fail on an exception in this case?
  • AttributeRouting does not work
  • How to draw rectangles and assign macros to them from VBA?
  • named parameter to npm run script
  • Java EE 6 Login module
  • Is this prime generator inefficient C++?
  • How to wrap object in a Promise?
  • cast stl::vector containing pointers to stl::vector containing constant pointers
  • Insert Path of a file with \\\\ in mysql using java
  • pandas parse csv with left and right quote chars
  • preg_replace
  • How to get Apache XML-RPC 3.1.3 compliance (ISO date format along with time zone) in Java 1.6
  • Simple Factory with reflection C#
  • Tools for understanding HTML layout
  • Where these are stored?
  • CSS bleed-through with cfinput type=“datefield”
  • how do i write assembly code from c#?
  • How do I open a C file with a relative path?
  • Use of this Javascript
  • C++ Partial template specialization - design simplification
  • Initializer list vs. initialization method
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • How to get next/previous record number?
  • Circular dependency while pushing http interceptor
  • Python: how to group similar lists together in a list of lists?
  • AngularJs get employee from factory
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • How does Linux kernel interrupt the application?