call web service w/javascript working fine except when I ....

Last post 03-28-2009 10:02 AM by akmailin. 3 replies.

Sort Posts:

  • call web service w/javascript working fine except when I ....

    03-17-2009, 8:41 PM
    • Member
      21 point Member
    • ericnickus2
    • Member since 03-15-2009, 1:19 AM
    • Posts 9

     I have a javascript that returns an array of json from a webservice.   working fine to return and output the array.   But I needed to add a section of html that actually calls up a flash audioplayer.   It seems that I am doing something wrong here ;  PLUS+ I am new to asp.net  (java developer) and I wonder if you could take one minute to give me any advice on my coding, I know it's probably pretty bad.  here's the default page that I cant get the player to work on.  It's supposed to output a speaker symbol that you click on and it plays music.  It works fine in regular asp.net with the format html thing inthe code behind.

    %@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <%@ Register assembly="AjaxDataControls" namespace="AjaxDataControls" tagprefix="AjaxData" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    <script type="text/javascript"
    src="StringBuilder.js">
    </script>
     <script language="javascript" type="text/javascript" >

         function pageLoad(sender, args) {


         }
         /* quick getElement reference */
         function $() {
             var elements = new Array();
             for (var i = 0; i < arguments.length; i++) {
                 var element = arguments[i];
                 if (typeof element == 'string')
                     element = document.getElementById(element);
                 if (arguments.length == 1)
                     return element;
                 elements.push(element);
             }
             return elements;
         }

         function OnComplete(result) {
             if (result != null){

                 var _songs = result;
               
                 for (i = 0; i < 11; i++) {
            
               

                         var Title = _songs[i].title;
                         var Artist = _songs[i].artist;
                         var url1 = _songs[i].auditionurl;
                         var url2 = _songs[i].downloadurl1;

                           
    ////////////////////// format the auditionurl
                     
                           var sb = new StringBuilder();
                           sb.append("<");
                           sb.append("scr");
                           sb.append("ipt");
                           sb.append(" language=JavaScript src=www.solicitedmaterial.net/audioplayerfolder/audio-player.js type=text/javascript><");
                           sb.append("/ script><");
                           sb.append(" object type=application/x-shockwave-flash data=http://www.solicitedmaterial.net/audioplayerfolder/player.swf");
                           sb.append("id=audioplayer");
                           sb.append(i);
                           sb.append(" height=16 width=350><");
                           sb.append("param name=movie value=solicitedmaterial.net/audioplayerfolder/player.swf /><");
                           sb.append("param name=FlashVars value=playerID=");
                           sb.append(i);
                           sb.append("&");
                           sb.append("amp;soundFile=http://www.solicitedmaterial.net/audio-audition_files/");
                           sb.append(url1);
                           sb.append("/><");
                           sb.append("param name=quality value=high /><");
                           sb.append("param name=menu value=false /><");
                           sb.append(" param name=wmode value=transparent /><");
                           sb.append("object/>");

                           var urlout = sb.toString();
    //////////////////////////////////////////////////////
                  //the next line outputs 10 rows of data fine
     //     document.getElementById('AllData').innerHTML = Title + " " + Artist + "  " + url1 + "  " + url2 + "<br />" + document.getElementById('AllData').innerHTML;

    // when I do this: I get only part of one line and some funky DOM errors
          document.getElementById('AllData').innerHTML = Title + " " + Artist + "  " + urlout + "  " + url2 + "<br />" + document.getElementById('AllData').innerHTML;
            
                 }
             }
         }

         function OnTimeOut(arg) {
             alert("Time");
         }

         function OnError(arg) {
             alert("Error");
         }

         function Button2_onclick() {
             ret = Songservice.GetSongs(OnComplete, OnError, OnTimeOut);
         }

     </script>

    </head>

    <body bgcolor="White">
        <form id="form1" runat="server">
     
        <asp:ScriptManager ID="ScriptManager1" runat="server" >
            <Services>
                <asp:ServiceReference Path="Songservice.asmx" />
                  </Services>
        </asp:ScriptManager>
              <input id="Button2" type="button" value="ShowData" OnClick="Button2_onclick()" />
              
              <div id="AllData"></div>
                <br /><br />
                <div id ="messageArea"></div>      
         
        </form>
    </body>
    </html>

     

    heres the web service

     

    sing System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Collections;
    using System.Web.Services.Protocols;
    using System.Configuration;
    using System.Data.SqlClient;




    /// <summary>
    /// Summary description for Songservice
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
     [System.Web.Script.Services.ScriptService]
          public class Songservice : System.Web.Services.WebService {
      
        private string strConn = "";
       
        public Songservice () {

            strConn = ConfigurationManager.ConnectionStrings["conn2"].ConnectionString;

            //Uncomment the following line if using designed components
            //InitializeComponent();
        }

       
       

        [WebMethod]
        public Song[] GetSongs() {
    //public string[] GetSongs(){
            SqlConnection cnn = new SqlConnection(strConn);
            cnn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cnn;
            cmd.CommandText = "SELECT title, artist, auditionurl, downloadurl1 FROM songbytitle";
            //SqlParameter artst = new SqlParameter("@artst", partistField);
            //cmd.Parameters.Add(artst);
            SqlDataReader reader = cmd.ExecuteReader();
            List<Song> list = new List<Song>();
          
            while (reader.Read())
            {
                Song sng = new Song();
                sng.title = reader.GetString(0);
                sng.artist = reader.GetString(1);
                sng.auditionurl = reader.GetString(2);
                sng.downloadurl1 = reader.GetString(3);
           
                list.Add(sng);
            }

            reader.Close();
            cnn.Close();
            return list.ToArray();
        //    return new string[]{ "Bill", "Scott", "Brad" }; 
        }    // end getSongs method
      

    }
     

     

    p.s. in case anyone is actually trying to use my example to get a working web service call that returns data from a remote server, I did define a class file called song.cs and the StringBuilder.js is pretty common , it;s out there.

     

    Thanks 

  • Re: call web service w/javascript working fine except when I ....

    03-18-2009, 1:03 AM
    • Participant
      770 point Participant
    • akmailin
    • Member since 02-25-2009, 11:38 PM
    • Posts 130

    Please see if this is of any help

    http://forums.asp.net/t/1390930.aspx

    Please revert back in case of any issues;
    Also If this is your answer Please don't forget to mark as answer.
  • Re: call web service w/javascript working fine except when I ....

    03-18-2009, 2:11 AM
    • Member
      21 point Member
    • ericnickus2
    • Member since 03-15-2009, 1:19 AM
    • Posts 9

     ty for your help.  I think it's my fault for not making myself clearer I'm afraid, because now I am having a problem outputting html (any html) like this isn't working for me:

     documentGetElementById('AllData').innerHTML = "<table>";

    then in my for loop 

     documentGetElementById('AllData').innerHTML = "<tr><td>" + javascriptfield + "</td></tr>";

    nothing like that is working for me.  

    Is it because it's deserialized json (coming back from the webservice

    ?  if so , why does it default to json (the array that is returned)

    and also'  why do I find myself having to repeat document.getElementById('AllData').innerHTML; at the end of the line to get output??   seems like a simple thing to do , but everything in javascript is so picky]

     

     

  • Re: call web service w/javascript working fine except when I ....

    03-28-2009, 10:02 AM
    • Participant
      770 point Participant
    • akmailin
    • Member since 02-25-2009, 11:38 PM
    • Posts 130

    ericnickus2:
    documentGetElementById('AllData').innerHTML = "<tr><td>" + javascriptfield + "</td></tr>";

     

    First replace the code by

    documentGetElementById('AllData').innerHTML += "<tr><td>" + javascriptfield + "</td></tr>"

     

    Please revert back in case of any issues;
    Also If this is your answer Please don't forget to mark as answer.
Page 1 of 1 (4 items)