2011-05-16 67 views
1

我在我的項目WCF服務(ProductService.svc在forder服務)地址:如何從.NET 3.5中的javascript調用WCF服務?

using System; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Activation; 
using System.ServiceModel.Web; 
using System.Web.Services; 
using System.Collections.Generic; 

namespace Application.Services { 
    [ServiceContract(Namespace = "")] 
    [AspNetCompatibilityRequirements(RequirementsMode 
= AspNetCompatibilityRequirementsMode.Allowed)] 
    public class ProductService 
    { 
     [WebMethod] 
     [OperationContract] 
     public static string GetMainGridProductHtml() 
     { 
      return "Helo World :)"; 
     } 
    } 
} 

並嘗試從調用服務的JavaScript文件:

<script type="text/javascript"> 
Application.Services.ProductService.GetMainGridProductHtml(ResultLoadTableMainGridHtml, ErrorLoadTableMainGridHtml); 

function ResultLoadTableMainGridHtml(html) { 
    debugger; 
    alert("Ok"); 
} 

function ErrorLoadTableMainGridHtml() { 
    debugger; 
    alert("Error"); 
} 

function NewAddBtn() { 
    debugger; 
    alert("Yuppi"); 
} 
</script> 

它不會工作:微軟JScript運行錯誤:'應用程序'未定義 我該如何管理?

+0

你在頁面上有'asp:ServiceReference'的'asp:ScriptManager'? – Menahem 2011-05-16 07:05:19

回答

0

我想類似的代碼你的,我認爲 因爲你使用的是空的命名空間

[ServiceContract(Namespace = "")] 

爲代理產生的JavaScript是訪問使用

ProductService.GetMainGridProductHtml 

直接且沒有命名空間Application.Services

ED IT:代碼示例:

svc文件

<%@ ServiceHost Language="C#" Debug="true" 
Service="Application.Services.ProductService" 
Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" 
CodeBehind="ProductService.svc.cs" %> // your cs file name may be different 
在你的aspx頁面

你應該有腳本管理和參考 (改變基準爲您服務):

<asp:ScriptManager ID="ScriptManager1" runat="server"> 
      <Services> 
       <asp:ServiceReference Path="~/Service1.svc" /> 
      </Services> 
     </asp:ScriptManager> 

在你的web.config中,當你添加啓用WCF的WCF服務時,VS2010添加WCF和AJAX的服務模型設置: 你的類名和命名空間可能不同,這是在我的機器上創建的:

<system.serviceModel> 
     <behaviors> 
      <endpointBehaviors> 
      <behavior name="WebApplication1.Service1AspNetAjaxBehavior"> 
       <enableWebScript /> 
      </behavior> 
      </endpointBehaviors> 
     </behaviors> 
     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
      multipleSiteBindingsEnabled="true" /> 
     <services> 
      <service name="WebApplication1.Service1"> 
      <endpoint address="" 
       behaviorConfiguration="WebApplication1.Service1AspNetAjaxBehavior" 
       binding="webHttpBinding" contract="WebApplication1.Service1" /> 
      </service> 
     </services> 
     </system.serviceModel> 

這一切的地方,你應該能夠調用客戶端上的 usign ProductService.GetMainGridProductHtml()服務。
(你也可以看到它在調試器監視窗口,如你預期)

,以確保您得到正確genereted的JavaScript,瀏覽到服務+/jsdebug的URL 即http://localhost:3953/Service1.svc/jsdebug

+0

我已經試過了,不起作用。 '.svc'文件中的 – 2011-05-16 08:13:25

+0

你有一個'Factory'指令嗎? – Menahem 2011-05-16 08:16:05

+0

Loks like this:<%@ ServiceHost Language =「C#」Debug =「true」Service =「Application.Services.ProductService」CodeBehind =「ProductService.svc.cs」%> 但是,當我嘗試查看Watch時,根本看不到我的服務和命名空間。 – 2011-05-16 08:30:55