wcf与jquery和ajax [英] wcf with jquery and ajax

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

问题描述

大家好..



i wana通过jquery在HTMl页面中消耗我的wcf服务...为此...我看到了一些代码,但它无效。我得到了错误的地方..



这里我的代码



IService1.svc

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

命名空间 WcfService1
{


[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json,RequestFormat = WebMessageFormat.Json,Method = GET,BodyStyle = WebMessageBodyStyle.WrappedRequest) ]
列表< RunDto> GetTeamData( int teamId, int weekNumber);
}

[DataContract]
public class RunDto
{
public string name1 = SampleName;
[DataMember]
public string name
{
get { return name1; }
set {name1 = value ; }
}

}

}





Service1.svc .cs



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

namespace WcfService1
{

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1:IService1
{
List< RunDto> ls = new 列表< RunDto>();

public 列表< RunDto> GetTeamData( int teamId, int weekNumber)
{
RunDto rn = new RunDto();
rn.name = teamId + s + weekNumber;
ls.Add(rn);
return ls;

}
}
}





web.config



 < ?xml version =    1.0>  
< configuration>

< system.web>
< compilation debug = true targetFramework = 4.0 />
< / system.web >
< system.serviceModel>
< services>
< service name = SampleService behaviorConfiguration = ServiceBehaviour >
< endpoint address = binding = webHttpBinding contract = IService1 name = samendpoint behaviorConfiguration = AjaxBehaviour >

< / endpoint >
< endpoint address = mex binding = mexHttpBinding contract = IMetadataExchange />
< / 服务 >
< / services >

< behavior>
< serviceBehaviors>
< behavior name = ServiceBehaviour >
< serviceMetadata httpGetEnabled = true />
< serviceDebug includeExceptionDetailInFaults = true />
< / 行为 >
< / serviceBehaviors >
< endpointBehaviors>
< behavior name = AjaxBehaviour >
< webHttp />

< / 行为 >
< / endpointBehaviors >
< / 行为 >
< serviceHostingEnvironment multipleSiteBindingsEnabled = true aspNetCompatibilityEnabled = true />
< / system.serviceModel >
< system.webServer>
<! - < modules runAllManagedModulesForAllRequests = true /> - >
< directoryBrowse enabled = true />
< / system.webServer >

< / configuration >







在Html页面中调用jquery



函数GetTeamData(teamId,weekNumber){
$ .ajax(
{
async true
type: GET
contentType: application / json; charset = utf-8
url: ../../服务/ RunningBarbusService.svc / GetTeamData
dataType: json
数据:' {teamId:' + teamId + ' ,weekNumber:' + weekNumber + ' }'
成功:函数(内容){
DisplayRun(地图,内容);
}
});
}





第1次虽然只是因为它的获取错误而感到惊讶这个



错误:无法从 http: // 获取元数据  localhost:50807 / Service1.svc如果这是您有权访问的Windows(R)Communication Foundation服务,请检查您是否已在指定地址启用元数据发布。有关启用元数据发布的帮助,请参阅http://go.microsoft.com/fwlink/?LinkId=65455上的MSDN文档.WS-Metadata Exchange错误URI:http:// localhost:50807 / Service1.svc元数据包含无法解析的引用:'http:// localhost:50807 / Service1.svc'。内容类型application / soap + xml;服务http:// localhost:50807 / Service1.svc不支持charset = utf-8。客户端和服务绑定可能不匹配。远程服务器返回错误:(415)不支持的媒体类型.HTTP GET错误URI:http:// localhost:50807 / Service1.svc HTML文档不包含Web服务发现信息。 





i需要从html页面消耗我的服务../。任何可能的方式...或对我的代码的任何修改..



感谢..提前

解决方案

< blockquote> .ajax(
{
async true
类型: GET
contentType: application / json; charset = utf-8
url: ../../ Services / RunningBarbusService.svc / GetTeamData
dataType: json
data:' { teamId:' + teamId + ' ,weekNumber:'' + weekNumber + ' }'
成功:功能(内容){
DisplayRun(地图,内容);
}
});
}





第1次虽然只是因为它的获取错误而感到惊讶这个



错误:无法从 http: // 获取元数据  localhost:50807 / Service1.svc如果这是您有权访问的Windows(R)Communication Foundation服务,请检查您是否已在指定地址启用元数据发布。有关启用元数据发布的帮助,请参阅http://go.microsoft.com/fwlink/?LinkId=65455上的MSDN文档.WS-Metadata Exchange错误URI:http:// localhost:50807 / Service1.svc元数据包含无法解析的引用:'http:// localhost:50807 / Service1.svc'。内容类型application / soap + xml;服务http:// localhost:50807 / Service1.svc不支持charset = utf-8。客户端和服务绑定可能不匹配。远程服务器返回错误:(415)不支持的媒体类型.HTTP GET错误URI:http:// localhost:50807 / Service1.svc HTML文档不包含Web服务发现信息。 





i需要从html页面消耗我的服务../。任何可能的方式...或对我的代码的任何修改..



感谢..提前


当你正在运行直接服务,它默认运行 WCFTestClient



WCFTestClient 不支持 webHttpBinding

这就是它抛出异常的原因。如果您将在 IIS 中托管服务,您可以直接浏览服务文件而不会出现任何错误(前提是没有错误)其他问题)。



因此,要测试 html 页面,只需运行 html 直接页面。

并在 jQuery中放入一些调试器 函数和签入浏览器( FireBug FireFox 中),如果它调用服务并按预期返回值或没有。



如果您有任何进一步的问题,请告诉我。


hello to all..

i wana consume my wcf service in HTMl pages through jquery... for this..i seen some code's but its not working .. i dint getting where am doing mistake..

here my code

IService1.svc

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

namespace WcfService1
{
 

    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        List<RunDto> GetTeamData(int teamId, int weekNumber);
    }

    [DataContract]
    public class RunDto
    {
        public string name1 = "SampleName";
        [DataMember]
        public string name
        {
            get { return name1; }
            set { name1 = value; }
        }
      
    }
   
}



Service1.svc.cs

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

namespace WcfService1
{
   
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1 : IService1
    {
        List<RunDto> ls = new List<RunDto>();

        public List<RunDto> GetTeamData(int teamId, int weekNumber)
        {
            RunDto rn = new RunDto();
            rn.name = teamId + " s " + weekNumber;
            ls.Add(rn);
            return ls;
           
        }
    }
}



web.config

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="SampleService" behaviorConfiguration="ServiceBehaviour">
        <endpoint address="" binding="webHttpBinding" contract="IService1" name="samendpoint" behaviorConfiguration="AjaxBehaviour">
       
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"   />
      </service>
    </services>
    
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
         <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="AjaxBehaviour">
          <webHttp/>
         
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <!--<modules runAllManagedModulesForAllRequests="true" />-->
    <directoryBrowse enabled="true" />
  </system.webServer>
  
</configuration>




jquery call in Html page

function GetTeamData(teamId, weekNumber) {
    $.ajax(
    {
        async: true,
        type: "GET",
        contentType: "application/json; charset=utf-8",
        url: "../../Services/RunningBarbusService.svc/GetTeamData",
        dataType: "json",
        data: '{"teamId":"' + teamId + '", "weekNumber":"' + weekNumber + '"}',
        success: function (content) {
            DisplayRun(map, content);
        }
    });
}



1st while excuting service only its getting error lik this

Error: Cannot obtain Metadata from http://localhost:50807/Service1.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address.  For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error    URI: http://localhost:50807/Service1.svc    Metadata contains a reference that cannot be resolved: 'http://localhost:50807/Service1.svc'.    Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:50807/Service1.svc.  The client and service bindings may be mismatched.    The remote server returned an error: (415) Unsupported Media Type.HTTP GET Error    URI: http://localhost:50807/Service1.svc    The HTML document does not contain Web service discovery information.



i need to consume my service from html page../. any possible ways... or any modifications to my code..

thanks.. in advance

解决方案

.ajax( { async: true, type: "GET", contentType: "application/json; charset=utf-8", url: "../../Services/RunningBarbusService.svc/GetTeamData", dataType: "json", data: '{"teamId":"' + teamId + '", "weekNumber":"' + weekNumber + '"}', success: function (content) { DisplayRun(map, content); } }); }



1st while excuting service only its getting error lik this

Error: Cannot obtain Metadata from http://localhost:50807/Service1.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address.  For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error    URI: http://localhost:50807/Service1.svc    Metadata contains a reference that cannot be resolved: 'http://localhost:50807/Service1.svc'.    Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:50807/Service1.svc.  The client and service bindings may be mismatched.    The remote server returned an error: (415) Unsupported Media Type.HTTP GET Error    URI: http://localhost:50807/Service1.svc    The HTML document does not contain Web service discovery information.



i need to consume my service from html page../. any possible ways... or any modifications to my code..

thanks.. in advance


As you are running the Service directly, it is running the WCFTestClient by default.

WCFTestClient does not support webHttpBinding.
That's why it is throwing the exception. If you will host the Service in IIS, you can browse the Service file directly without any errors (provided there are no other issues).

So, to test your html page, just run that html page directly.
And put some debuggers inside the jQuery functions and check in browser (FireBug in FireFox), if it calling the Service and returning values as expected or not.

Let me know, if you face any issues further.


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

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