繁体   English   中英

Web服务WCF和Javascript

[英]Web Service WCF and Javascript

我有2个项目(WCF项目和Asp.Net项目)。 在这里遵循了本教程。

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

我在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();

这是我的带有JavaScript代码的网页(aspx):

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

Brian,本地主机:52768 / Service1.svc显示

在此处输入图片说明

使用检查元素时出现此错误 在此处输入图片说明

我是这个领域的新手,对于我的英语不好,我感到抱歉。

我的解决方案中有2个项目,即WCF和一个应用程序控制台(GettingStartedHost),该项目将用来关闭Web服务。

我还在GettingStartedHost项目的app.config中添加了连接字符串,现在它可以正常工作了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM