使用Jquery调用WCF服务 [英] Call WCF service using Jquery

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

问题描述




i在特定情况下将WCF服务与jquery连接有问题。



我们按照步骤进行操作.. 。

1.我们创建了一个WCF服务(使用WCF类库),带有2个端点(basichttp绑定和wshttp绑定)。

2.现在我们发布服务,发布后我们得到3件事(a)bin文件夹(b)svc文件(c)webconfig

3.现在我们(我们在.net 3.5中有一个现有的网站)以下列方式在我们的网站上使用服务

(a)使用添加引用添加bin,

(b)使用SVC文件

(c)添加< system.servicemodel>我们的网站webconfig中的部分(从我们发布后获得的网络配置中选择)



当我们运行我们的网站时(窗口托盘显示我们的服务主机在aspnet本地服务器。)现在我想通过(a)jquery(b)代码(我们已经完成)使用服务。



当我们尝试通过Jquery连接并使用wcf服务时,它会显示500内部服务器错误或不支持的媒体类型错误(错误编码415)。



我们应用的设置我们的服务



(1)

Hi
i have problem with connecting WCF service with jquery in a specific condition.

Step we follow...
1. we create a WCF service (using WCF Class Library) with 2 endpoint (basichttp binding and wshttp binding).
2. Now we publish service, after publish we get 3 thing (a) bin folder (b)svc file (c) webconfig
3. now we (we have a existing website in .net 3.5) use service in our website in following manner
(a)Add bin using add reference,
(b)Use SVC file
(c)Add <system.servicemodel> section in our web site webconfig (pick from web config which we get after publishing)

when we run our website (the window tray show that our service host in aspnet local server.)
now i want to use service via (a) jquery (b) code behind (which we have done).

When we try to connect and use wcf service through Jquery sometime it show 500 internal server error or unsupported media type error(error codde 415).

Settings which we applied in our service

(1)

[AspNetCompatibilityRequirements(RequirementsMode =    AspNetCompatibilityRequirementsMode.Allowed)]
   public class Service1 : IService1
   {
  [WebInvoke(Method =POST, BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]

      public string Search(string name)
      {
       string result;
         .........
         return result;
      }

   }







please帮助....纠正我任何类型的错误。

(plz忽略任何英语语法错误)



提前致谢... ..




please help.... correct me for any type of error.
(plz ignore any english grammer mistake)

Thanks in advance....

推荐答案

首先让你的服务休息,这样你就可以获得普通的URL来访问。

First of all make your service Restful so that you can get plain URL to access.
[WebInvoke(Method =POST, BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, UriTemplate="/SearchMethod")]
//Now you can access this mehtod by the name you have given in uri template.
//like localhost:12345//YourService.svc/SearchMethod





在Web配置中您需要对跨域jquery进行一些额外的更改。

请参阅此.. 。

http://pranayamr.blogspot.in/2011/06/calling-cross- domain-wcf-service-using.html [ ^ ]



您可以通过两种方式实现跨域(据我所知)。

1.CORS - 跨源资源共享。

参考这篇文章......

http://www.nczonline.net/blog/2010/05/25 /跨域-ajax-with-cross-origin-resource-sharing / [ ^ ]



http://www.html5rocks.com/en/tutorials/cors/ [ ^ ]



2。 JsonP dataType

请参阅jsonp ...



http://forums.asp.net/t/1705528.aspx?How+to+call+Remote+Webservice +通过+ Jquery + JsonP [ ^ ]



http://www.jquery4u.com/json/jsonp-examples/ [ ^ ]



希望这个帮助

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

Pratik Bhuva



In web Config You need some additional changes for Cross domain jquery.
Refer This...
http://pranayamr.blogspot.in/2011/06/calling-cross-domain-wcf-service-using.html[^]

You can achieve cross domain by two ways(as per my knowledge).
1.CORS - Cross Origin Resource Sharing.
Refer this article...
http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/[^]
And
http://www.html5rocks.com/en/tutorials/cors/[^]

2.JsonP dataType
Refer this for jsonp...

http://forums.asp.net/t/1705528.aspx?How+to+call+Remote+Webservice+through+Jquery+JsonP[^]
And
http://www.jquery4u.com/json/jsonp-examples/[^]

Hope This Help
---------------
Pratik Bhuva


您可以查看下面提到的文章以获取更多信息。



You can check below mentioned article for more info.

// Function to call WCF  Service
function CallService() {


.ajax({
type:Type, // GET或POST或PUT或DELETE动词
url:Url, // 服务的位置
数据:数据, // 发送到服务器的数据
contentType:ContentType, // 发送到服务器的内容类型
dataType:DataType, // 来自服务器的预期数据格式
processdata:ProcessData, // 对错
成功:功能(msg){ // 在成功的服务电话上
ServiceSucceeded(msg);
},
错误:ServiceFailed // 服务调用失败时
});
}

函数ServiceFailed(结果){
alert(' 服务调用失败:' + result.status + ' ' + result.statusText);
Type = null ;
varUrl = null ;
数据= null ;
ContentType = null ;
DataType = null ;
ProcessData = null ;
}
.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; }





欲了解更多信息:使用jQuery调用WCF服务



另一个链接:使用jQuery在两个简单的步骤中使用RESTful服务



我希望这会对你有所帮助。



For more info : Calling WCF Services using jQuery

Another Link :Consume RESTful service using jQuery in 2 simple steps

I hope this will help to you.


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

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