2014-02-21 81 views
0

我有2个项目(WCF项目和Asp.Net项目)。我遵循这个教程hereWeb服务WCF和Javascript

在本教程中,我们将在一个项目中实现所有功能(请参阅源here)。

我在Web项目中引用了我的WCF项目。

我尝试在我的警报中显示一个值,但没有任何值。

这里是我的Service1.svc:

namespace WSSage100{ 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class Service1 : IService1 
{ 
    public string GetData(int value) 
    { 
     return string.Format("You entered: {0}", value); 
    } 

    public string GetData2(int value) 
    { 
     return string.Format("You entered: {0}", value); 
    } 


    public string[] GetUser(string Id) 
    { 
     return new User().GetUser(Convert.ToInt32(Id)); 
    } 

    public string GetTEST() 
    { 
     return "OKKKKKKKK"; 
    } 
} 

这里是IService1.cs:

namespace WSSage100 
{ 

[XmlSerializerFormat] 
[ServiceContract(Namespace ="WSSage100")] 
public interface IService1 
{ 

    [OperationContract] 
    [WebInvoke(Method = "GET",ResponseFormat = WebMessageFormat.Json)] 
    string GetData(int value); 


    [OperationContract] 
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)] 
    string GetData2(int value); 

    [OperationContract] 
    [WebInvoke(Method = "POST",BodyStyle = WebMessageBodyStyle.Wrapped,ResponseFormat = WebMessageFormat.Json)] 
    string[] GetUser(string Id); 

    [OperationContract] 
    [WebInvoke(Method = "GET",ResponseFormat = WebMessageFormat.Json)] 
    string GetTEST(); 

这里是我的网页(CSS)与JavaScript代码:

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head id="Head1" runat="server"> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script language="javascript" type="text/javascript" src="JavaScript/jquery-1.10.2.min.js"></script> 

<script type="text/javascript"> 

    var Type; 
    var Url; 
    var Data; 
    var ContentType; 
    var DataType; 
    var ProcessData; 

    function WCFJSON() { 
     var userid = "1"; 
     Type = "POST"; 
     Url = "Service1.svc/GetUser"; 
     Data = '{"Id": "' + userid + '"}'; 
     ContentType = "application/json; charset=utf-8"; 
     DataType = "json"; varProcessData = true; 
     CallService(); 
    } 

    function CallService() { 
     $.ajax({ 
      type: Type, //GET or POST or PUT or DELETE verb 
      url: Url, // Location of the service 
      data: Data, //Data sent to server 
      contentType: ContentType, // content type sent to server 
      dataType: DataType, //Expected data format from server 
      processdata: ProcessData, //True or False 
      success: function (msg) {//On Successfull service call 
       ServiceSucceeded(msg); 
      }, 
      error: ServiceFailed// When Service call fails 
     }); 
    } 

    function ServiceFailed(result) { 
     alert('Service call failed: ' + result.status + '' + result.statusText); 
     Type = null; 
     varUrl = null; 
     Data = null; 
     ContentType = null; 
     DataType = null; 
     ProcessData = null; 
    } 

    function ServiceSucceeded(result) { 
     if (DataType == "json") { 
      resultObject = result.GetUserResult; 

      for (i = 0; i < resultObject.length; i++) { 
       alert(resultObject[i]); 
      } 

     } 

    } 

    function ServiceFailed(xhr) { 
     alert(xhr.responseText); 

     if (xhr.responseText) { 
      var err = xhr.responseText; 
      if (err) 
       error(err); 
      else 
       error({ Message: "Unknown server error." }) 
     } 

     return; 
    } 

    $(document).ready(
     function() { 
      WCFJSON(); 
     } 
    ); 
</script> 
<style type="text/css"> 
    #form1 { 
     height: 255px; 
    } 
</style> 

</head> 
<body> 
    <form id="form1" runat="server"> 

    <div></div> 
    </form> 
</body> 
</html> 

这里是我的Web项目中的Service1.svc:

<%@ ServiceHost Language="C#" Debug="true" Service="WSSage100.Service1"%> 

这是我在我的Web项目的web.config:

<configuration> 
<system.web> 
    <compilation debug="true" targetFramework="4.5" /> 
    <httpRuntime targetFramework="4.5" /> 

</system.web> 

<system.serviceModel> 
    <client> 
     <endpoint address="http://localhost:52768/Service1.svc" binding="basicHttpBinding" 
      bindingConfiguration="BasicHttpBinding_IService1" contract="WebServiceSage100.IService1" 
      name="BasicHttpBinding_IService1" /> 
    </client> 
    <behaviors> 
     <serviceBehaviors> 
      <behavior name=""> 
       <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> 
       <serviceDebug includeExceptionDetailInFaults="false" /> 
      </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="EndpBehavior"> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
     multipleSiteBindingsEnabled="true" /> 
    <bindings> 
     <basicHttpBinding> 
      <binding name="BasicHttpBinding_IService1" /> 
     </basicHttpBinding> 
    </bindings> 

    <services> 
    <service name="WSSage100.Service1"> 
     <endpoint address="" binding="webHttpBinding" contract="WSSage100.IService1"> 
     </endpoint> 
    </service> 
    </services> 

    </system.serviceModel> 
</configuration> 

布莱恩为localhost:52768/Service1.svc显示

enter image description here

当我使用检查元素我有这样的错误 enter image description here

我是这个领域的新手,对不起我的英文不好。

+0

在控制台中的任何错误? – iJade

+0

我有任何错误。 – Jayce

+0

如果您在浏览器地址栏中输入http://localhost:52768/Service1.svc,您会得到什么回应? – Brian

回答

0

我在我的解决方案中有两个项目,WCF和一个应用程序控制台(GettingStartedHost),它们将会关闭Web服务。

我还在GettingStartedHost项目的app.config中添加了连接字符串,现在它运行良好。