无法通过HTMl访问wcf,javascript [英] Unable to access wcf through HTMl,javascript

查看:65
本文介绍了无法通过HTMl访问wcf,javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我是WCF的新手,我已经创建了启用了ajax的wcf服务,但是当我尝试通过Javascript访问它时,我没有得到任何回复。



请帮帮我





HTML code:

Hi everyone,
I am new to WCF,i have created the ajax enabled wcf service but when i try to access it through Javascript i am not getting any response.

please help me


HTML code:

<!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>
    <title></title>
    <script type="text/javascript" language="javascript" src ="jquery.min.js">

    </script>


</head>
<body>
<form id="form1" runat="server">
<input id="Text1" type="text" />
<input id="Button1" type="button" value="Submit" onclick="HelloWorld()"/>
<script type="text/javascript">
    function HelloWorld() {
        var a = document.getElementById('Text1').value;
        
        $.ajax({
            type: "POST",
            url: "http://localhost:59191/Service1.svc/DoWork",
            data: "{'id':'" + a + "'}",
            contentType: "application/json; charset=utf-8 ",
            dataType: "json",
            success: OnSuccessCall,
            error: OnErrorCall
        });

    }

    function OnSuccessCall(response) {
        alert(response.d);
    }
    function OnErrorCall(response) {
        alert("Error");
    }

</script>

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







WCF服务:






WCF Service:

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

namespace Wcftestthrajax
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1
    {
        // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
        // To create an operation that returns XML,
        //     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
        //     and include the following line in the operation body:
        //         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";

        [OperationContract]
        [WebGet(UriTemplate = "", ResponseFormat = WebMessageFormat.Json)]
        public string DoWork(string id)
        {
            // Add your operation implementation here
            return "hello wcf" +id ;
        }

        // Add more operations here and mark them with [OperationContract]
    }
}





Webconfig:







Webconfig:


<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="Wcftestthrajax.Service1AspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"

            multipleSiteBindingsEnabled="true" />
<services>
<service name="Wcftestthrajax.Service1">
<endpoint address="" behaviorConfiguration="Wcftestthrajax.Service1AspNetAjaxBehavior"

                    binding="webHttpBinding" contract="Wcftestthrajax.Service1" />
</service>
</services>
</system.serviceModel>
</configuration>

推荐答案

.ajax({
type:POST,
url:http:// localhost:59191 / Service1。 svc / DoWork,
数据:{'id':'+ a +'},
contentType:application / json; charset = utf-8,
dataType:json,
成功:OnSuccessCall,
错误:OnErrorCall
});

}

函数OnSuccessCall(响应){
alert(response.d);
}
函数OnErrorCall(响应){
alert(Error);
}

< / script >

< / form >
< / body >
< / html >
.ajax({ type: "POST", url: "http://localhost:59191/Service1.svc/DoWork", data: "{'id':'" + a + "'}", contentType: "application/json; charset=utf-8 ", dataType: "json", success: OnSuccessCall, error: OnErrorCall }); } function OnSuccessCall(response) { alert(response.d); } function OnErrorCall(response) { alert("Error"); } </script> </form> </body> </html>







WCF服务:






WCF Service:

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

namespace Wcftestthrajax
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1
    {
        // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
        // To create an operation that returns XML,
        //     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
        //     and include the following line in the operation body:
        //         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";

        [OperationContract]
        [WebGet(UriTemplate = "", ResponseFormat = WebMessageFormat.Json)]
        public string DoWork(string id)
        {
            // Add your operation implementation here
            return "hello wcf" +id ;
        }

        // Add more operations here and mark them with [OperationContract]
    }
}





Webconfig:







Webconfig:


<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="Wcftestthrajax.Service1AspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"

            multipleSiteBindingsEnabled="true" />
<services>
<service name="Wcftestthrajax.Service1">
<endpoint address="" behaviorConfiguration="Wcftestthrajax.Service1AspNetAjaxBehavior"

                    binding="webHttpBinding" contract="Wcftestthrajax.Service1" />
</service>
</services>
</system.serviceModel>
</configuration>


更改您的服务注释,如下所示。请确保您的服务是否正常工作

Change Your Service annotations like below.Please make sure that your service is working or not
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate ="/DoWork")]
 [OperationContract]





希望这有助于



Hope this helps


这篇关于无法通过HTMl访问wcf,javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆