从jquery调用wcf服务 [英] calling wcf service from jquery

查看:82
本文介绍了从jquery调用wcf服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Internet Explorer中运行我的客户端应用程序时,我得到了输出。



它称之为服务,但是当我在Google Chrome和Firefox中运行相同的客户端应用程序时,会出现404错误。



你能解决我遇到的问题吗?



web.config



When I run My client application in Internet explorer, I got the output.

It called the service, but when I run the same client application in Google chrome and Firefox its getting 404 error.

Can you give some solution for the problem I am facing?

web.config

<system.serviceModel>

      <services>
      <service name="WcfService3codepro.Service1" behaviorConfiguration="MyServiceBehaviour">
        <endpoint address="" binding="webHttpBinding" contract="WcfService3codepro.IService1" behaviorConfiguration="MyEndpointBehavior" />
      </service>
    </services>
    <bindings>

    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the value below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>

    <endpointBehaviors>
        <behavior name="MyEndpointBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true" />
        <defaultDocument>
            <files>
                <add value="Service1.svc" />
            </files>
        </defaultDocument>
  </system.webServer>

</configuration>





interface





interface

[OperationContract]
      [WebInvoke(Method = "*", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
      string MyFunction(string Count);
      // TODO: Add your service operations here
  }





class



class

[AspNetCompatibilityRequirements(RequirementsMode
    = AspNetCompatibilityRequirementsMode.Allowed)]
   public class Service1 : IService1
   {
       public string MyFunction(string Count)
       {
           return "The Name you passed  is: " + Count.ToString();
       }

   }





客户端jquery代码



client jquery code

<head runat="server">
    <title></title>
    <script src="Scripts/jquery-2.0.3.min.js"></script>
    <script type="text/javascript" lang="ja">
        var counter = "karthik";

        function CallMyService() {
            $.ajax({
                type: "POST",
                url: "http://localhost/WcfService3codepro/Service1.svc/MyFunction",
                data: '{"Count": "' + counter + '"}',
                contentType: "application/json",
                success: ServiceSucceeded,
                error: ServiceFailed
            });
        }
        function ServiceFailed(result) {

            Log('Service call failed: ' + result.status + '  ' + result.statusText);
        }
        function ServiceSucceeded(result) {
            var resultObject = result.MyFunctionResult;
            Log("Success: " + resultObject);
        }

        function Log(msg) {
            $("#logdiv").append(msg + "<br />");
        }

    </script>
</head>




<body>
    <form id="form1" runat="server">
    <div>
     <input id="Button1" type="button" value="Execute" onclick="CallMyService();" />
    <div id="logdiv"></div>
    </div>
        <asp:Label ID="lblmsg" runat="server" />
    </form>
</body>

推荐答案

.ajax({
type:POST,
url:http://localhost/WcfService3codepro/Service1.svc/MyFunction,
data:'{Count:'+ counter +'}',
contentType: application / json,
成功:ServiceSucceeded,
错误:ServiceFailed
});
}
函数ServiceFailed(结果){

Log('服务调用失败:'+ result.status +''+ result.statusText);
}
函数ServiceSucceeded(result){
var resultObject = result.MyFunctionResult;
Log(Success:+ resultObject);
}

函数Log(msg){
.ajax({ type: "POST", url: "http://localhost/WcfService3codepro/Service1.svc/MyFunction", data: '{"Count": "' + counter + '"}', contentType: "application/json", success: ServiceSucceeded, error: ServiceFailed }); } function ServiceFailed(result) { Log('Service call failed: ' + result.status + ' ' + result.statusText); } function ServiceSucceeded(result) { var resultObject = result.MyFunctionResult; Log("Success: " + resultObject); } function Log(msg) {


(#logdiv)。append(msg +< br / > );
}

< / script >
< / head >
("#logdiv").append(msg + "<br />"); } </script> </head>




<body>
    <form id="form1" runat="server">
    <div>
     <input id="Button1" type="button" value="Execute" onclick="CallMyService();" />
    <div id="logdiv"></div>
    </div>
        <asp:Label ID="lblmsg" runat="server" />
    </form>
</body>


我在15天之前遇到了同样的问题。

和我google了这么多,发现如果我在IIS服务器上托管它可能会有效。

尝试在IIS中托管它并检查会发生什么。



并检查请求标头请求标头可能使用OPTION方法而不是GET / POST发送请求。请尝试探索。

将您的合同方法更改为
I was getting the same problem just 15 days before.
And i googled it so much and found that it may work if i hosted it on IIS server.
try hosting that in IIS and check what happens.

And check the request header the request header might sending request with OPTION method instead of GET/POST.Try to explore on that also.
change your contract method to
[WebInvoke(Mehtod="*",...]





希望这有助于你。

-----------------------

Pratik Bhuva



Hope This Helps You.
-----------------------
Pratik Bhuva


这篇关于从jquery调用wcf服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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