如何从Jquery调用wcf服务 [英] How to call wcf service from Jquery

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

问题描述

Hello Everyone,



我需要一些帮助来解决如何从JQuery调用托管WCF服务



我需要托管这项服务,并从另一个应用程序调用Jquery的托管WCF服务...



我的代码如下..



IService1.cs



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Runtime.Serialization;
使用 System.ServiceModel;
使用 System.Text;
使用 System.ServiceModel.Web;

namespace WCF_REST_SOAP_JQuery
{
// < span class =code-comment>注意:您可以使用重构菜单上的重命名命令在代码和配置文件中一起更改接口名称IService1。
[ ServiceContract]
public interface IService1
{
[OperationContract]
string SayHello( string name);
}
}





Service1.svc



< pre lang =c#> 
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Runtime.Serialization;使用System.ServiceModel
;
使用System.Text;
使用System.ServiceModel.Web;
使用System.ServiceModel.Activation;


命名空间WCF_REST_SOAP_JQuery
{

[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
公共类Service1:IService1
{
[WebInvoke(Method =GET,ResponseFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Wrapped)]
公共字符串SayHello(字符串名称)
{
返回字符串。格式(Hello {0},名称);
}
}
}





网络。 config

 <   configuration  >  
< system。网络 >
< 编译 debug = true targetFramework = 4.0 / >
< / System.Web程序 >

< system.serviceModel >
< 服务 >
< service name = WCF_REST_SOAP_JQuery.Service1

behaviorConfiguration = ServiceBehaviour >
< ; endpoint 地址 < span class =code-keyword> = JSON 绑定 = webHttpBinding 合同 = WCF_REST_SOAP_JQuery.IService1

< span class =code-attribute> behaviorConfiguration = JSONEndpointBehaviour

bindingConfiguration = name = RESTEP > < / endpoint >

< endpoint 地址 = SOAP binding = basicHttpBinding 合同 = WCF_REST_SOAP_JQuery.IService1

名称 = 基本 > < / endpoint >

< endpoint 地址 = mex binding = mexHttpBinding 合同 = IMetadataExchange > < / endpoint >

< / service >
< / services >
< 行为 >
< endpointBehaviors >
< 行为 名称 = JSONEndpointBehaviour >
< webHttp / >
< / BEH avior >
< / endpointBehaviors >

< serviceBehaviors >
< behavior name = ServiceBehaviour >
< serviceMetadata httpGetEnabled = true / >
< < span class =code-leadattribute> serviceDebug includeExceptionDetailInFaults = false / >
< / behavior >
< / serviceBehaviors >
< / behavior >
< Servi大街ceHostingEnvironment multipleSiteBindingsEnabled = true aspNetCompatibilityEnabled = false / >
< /system.serviceModel >
< system.webServer >
< < span class =code-leadattribute> modules runAllManagedModulesForAllRequests = true / > ;
< directoryBrowse 已启用 = true / >
< httpProtocol >
< customHeaders >
< add name = Access-Control-Allow-Origin value = * / >
< add 名称 = 访问权限 - Control-Allow-Headers value = Origin,X-Requested-With,Content-Type,Accept / >
< add name = Access-Control-Allow-Methods value = GET, POST / >
< / customHeaders >
< / httpProtocol >
< / system.webServer >
< span class =code-keyword><
/ configuration >







请帮忙。

解决案

请参考此链接使用jQuery调用WCF服务 [ ^ ]



http://www.c-sharpcorner.com/uploadfile/sridhar_subra/consuming-wcf-asmx-rest-service-using-jquery/ [ ^ ]



http: //www.aspdotnet-suresh.com/2013/12/call-wcf-service-from-jquery-ajax-json-example-aspnet.html [ ^ ]


 

.ajax({
url: https://192.168.1.11 /UserService-20120830/GetUserService.svc/REST /
类型: post
dataType: json
data:{ },





它可能对你有帮助

从Jquery收到一个wcf服务 [ ^ ]


Hello Everyone,

I need some help in how to call hosted WCF service from JQuery

I need to host this service and call the hosted WCF service from Jquery from another application...

My code as below..

IService1.cs

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

namespace WCF_REST_SOAP_JQuery
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]    
    public interface IService1
    {
        [OperationContract]
        string SayHello(string name);
    }
}



Service1.svc

<pre lang="c#">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;


namespace WCF_REST_SOAP_JQuery
{
    
    [ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]   
    public class Service1 : IService1
    {
        [WebInvoke(Method="GET",ResponseFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Wrapped)]
        public string SayHello(string name)
        {
            return string.Format("Hello {0}", name);
        }
    }
}



Web.config

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

    <system.serviceModel>
      <services>
        <service name="WCF_REST_SOAP_JQuery.Service1"

                 behaviorConfiguration="ServiceBehaviour">
          <endpoint address="JSON" binding="webHttpBinding" contract="WCF_REST_SOAP_JQuery.IService1"

                    behaviorConfiguration="JSONEndpointBehaviour"

                    bindingConfiguration="" name="RESTEP"></endpoint>

          <endpoint address="SOAP" binding="basicHttpBinding" contract="WCF_REST_SOAP_JQuery.IService1"

                     name="basic"></endpoint>

          <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
          
        </service>
      </services>
        <behaviors>
          <endpointBehaviors>
            <behavior name="JSONEndpointBehaviour">              
              <webHttp/>
            </behavior>
          </endpointBehaviors>
          
            <serviceBehaviors>
                <behavior name="ServiceBehaviour">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="false" />
    </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
        <add name="Access-Control-Allow-Methods" value= "GET, POST"/>
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>




please some one help in this.

解决方案

refer this link Calling WCF Services using jQuery[^]

http://www.c-sharpcorner.com/uploadfile/sridhar_subra/consuming-wcf-asmx-rest-service-using-jquery/[^]

http://www.aspdotnet-suresh.com/2013/12/call-wcf-service-from-jquery-ajax-json-example-aspnet.html[^]



.ajax({ url: "https://192.168.1.11/UserService-20120830/GetUserService.svc/REST/", type: "post", dataType: "json", data: {},



It might help you
Call a wcf service from Jquery[^]


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

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