topical media & game development

talk show tell print

professional-javascript-16-LiveConnectExample2.htm / htm

          <title>LiveConnect Example</title>
          <script type="text/javascript">
                  function httpPost(sURL, sParams) {
                      var oURL = new;
                      var oConnection = oURL.openConnection();
                      oConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");                
                      var oOutput = new;
                      var sLine = "", sResponseText = "";
                      var oInput = new;                                
                      sLine = oInput.readLine();
                      while (sLine != null){                                
                          sResponseText += sLine + "\n";
                          sLine = oInput.readLine();
                      return sResponseText;                         
                  function addPostParam(sParams, sParamName, sParamValue) {
                      if (sParams.length > 0) {
                          sParams += "&";
                      return sParams + encodeURIComponent(sParamName) + "=" 
                                     + encodeURIComponent(sParamValue);
                  function getServerInfo() {
                      var sParams = "";
                      sParams = addPostParam(sParams, "name", "Nicholas");
                      sParams = addPostParam(sParams, "book", "Professional JavaScript");
                      var sResponseText = httpPost("http://localhost/reflectpost.php", sParams);
                      alert("Response text is: " + sResponseText);
          <p>Click the button to make a call to the server using LiveConnect.
          This example will only work when run in a Web server environment. You'll need
          to change the URL <code>http://localhost/reflectpost.php> to point to a valid
          file on the server. If this works, you should see the same values that
          were POSTed to the URL.</p>
          <input type="button" onclick="getServerInfo()" value="Get Server info" />

(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.