2010-02-20 84 views
1

我建立了一个基本的客户端回调到WCF服务。请参阅以下示例代码。页面基于ajax客户端回调重新加载到WCF

使用连接到资源管理器,你可以看到一个http过滤器查看时

: 1. service1.svc/JS工作正常,并返回正确的Java脚本的浏览器 2. serrvice1.svc作品,并返回一个适当的JSON数据。 3.电话很好,使用警报,而不是更新div信息我得到的数据。

但是之后在警报之后确定页面然后从头开始重新加载。

不知道为什么?有什么想法?

代码:

<%@ 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"> 

<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> 

回答

0

不知道这是否适用于按钮为好,但对于链接至少,你需要在onclick事件返回false。试试这个:

<button id="button1" onclick="testme();return false;" >TestMe</button> 
+0

是的,这做到了。 你能解释一下背后的逻辑吗? – Eyal 2010-02-20 14:31:17

+0

返回false指示事件取消。再一次,我不记得按钮是如何工作的,但我想通常点击一个按钮会回复表单。通过返回false,你基本上告诉按钮不要做它通常做的事情。 – CodingInsomnia 2010-02-20 15:41:59

0

确定,需要考虑几件事情:

  1. 确保您暴露启用了Web的http在Web服务绑定端点:

    <endpoint address="" 
          binding="webHttpBinding" 
          contract="Namespce.IServiceContract" /> 
    
  2. 请确保您在.svc标记中使用WebScriptServiceHostFactory工厂:

    <%@ ServiceHost 
        Language="C#" 
        Debug="true"  
        Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" 
        Service="Namespace.Service1" 
        CodeBehind="Service1.svc.cs" %> 
    
  3. 您正在调用iMDsoft.Demo.SimpleService.GetBedList函数,但是您是否已验证在生成的JavaScript代理(http://example.com/service1.svc/js)中是否存在此函数?例如:

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

    会生成以下JavaScript代理方法:imdsoft.demo.iservice1.GetBedList