tags:

views:

29

answers:

2

Ive set up a basic client side callback to a WCF service. see sample code below.

when viewing using an http filter attached to the explorer you can see that: 1. service1.svc/js is working fine and return proper java script to the browser 2. serrvice1.svc works and returns a proper json data. 3. call is fine and using alert instead of updating the div info i get the data.

but then after OK on the alert the page is then reloaded from scratch.

cant understand why? got any idea?

code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Simple_ClientNetwork_Calls.WebForm1" %>

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>

</head>
<body >
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Scripts>
                <asp:ScriptReference Path="~/WebKit.js" />
            </Scripts>
             <Services>
                <asp:ServiceReference  Path="~/Service1.svc" />
            </Services>
        </asp:ScriptManager>
      <script type="text/javascript">
          var BedList = null;
          var status = null;

          //add a window.load handler to init any necessary controls
          Sys.Application.add_load(initializeControls);

          function initializeControls(e) {
              status = $get("status");
          }

          function testme() {
              document.bgColor = "#FF0000";
              getBedList();
          }

          function getBedList() {
              iMDsoft.Demo.SimpleService.GetBedList(GetBedListOnSuccess, GetBedListOnFailed);
          }


          function GetBedListOnSuccess(results, context, methodName) {

              var BedList = results
              status.innerText = "Complete" + BedList[0].Name;
          }

          function GetBedListOnFailed(results, context, methodName) {
              status.innerText = "GetBedListOnFailed: " + results.get_message();
          }

    </script>
    <button id="button1" onclick="testme();" >TestMe</button>
    <div id="status">
            </div>
     </div>
    </form>
</body>
</html>
A: 

Not sure if this applies to buttons as well, but at least for links, you need to return false in the onclick event. Try this:

<button id="button1" onclick="testme();return false;" >TestMe</button>
CodingInsomnia
yep, this did it.can you explain the logic behind that?
Eyal
Returning false instructs the event to cancel. Again, I don't quite remember how buttons work, but I suppose that clicking a button normally would do a postback of the form. By returning false, you essentially tell the button not to do what it usually does.
CodingInsomnia
A: 

Ok there are a few things to consider:

  1. Make sure that you expose a web http enabled binding endpoint in your web service:

    <endpoint address="" 
              binding="webHttpBinding" 
              contract="Namespce.IServiceContract" />
    
  2. Make sure that you use the WebScriptServiceHostFactory factory in your .svc markup:

    <%@ ServiceHost 
        Language="C#" 
        Debug="true"    
        Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" 
        Service="Namespace.Service1" 
        CodeBehind="Service1.svc.cs" %>
    
  3. You are calling the iMDsoft.Demo.SimpleService.GetBedList function but have you verified that such function exists in the generated javascript proxy (http://example.com/service1.svc/js)? For example:

    [ServiceContract(Namespace = "http://imdsoft.demo")]
    public interface IService1
    {
        [OperationContract]
        string GetBedList();
    }
    

    will generate the following javascript proxy method: imdsoft.demo.iservice1.GetBedList

Darin Dimitrov