跨域调用WCF服务

编程入门 行业动态 更新时间:2024-10-11 09:20:47
本文介绍了跨域调用WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个WCF服务,这是一个我要调用的方法:

[OperationContract] [WebInvoke(Method =POST,BodyStyle = WebMessageBodyStyle.Wrapped,ResponseFormat = WebMessageFormat.Json)] double?[] GetPoints(string tourname);

我通过WCF Test Client检查,工作正常

所以我需要从html页面调用这个方法。它应该从跨域的其他计算机工作。

我使用jQuery编写了一个html 1-6-2.min.js:

var varType; var varUrl; var varData; var varContentType; var varDataType; var varProcessData; function CallService(){ alert(CallService); $ .ajax({ type:varType,// GET或POST或PUT或DELETE verb url:varUrl,//服务位置 data:varData, /发送到服务器的数据 contentType:varContentType,//发送到服务器的内容类型 dataType:varDataType,//服务器预期的数据格式 processdata:varProcessData,// True或False success:function(msg){//成功的服务调用 ServiceSucceeded(msg); },错误:ServiceFailed //当服务调用失败时}); } function Start(){ varType =POST; varUrl =http:// localhost:1592 / TourService.svc / GetPoints; varData ='{tourname:customname}'; varContentType =application / json; charset = utf-8; varDataType =json; varProcessData = true; CallService(); } function ServiceSucceeded(result){ alert(ServiceSucceeded); alert(result); } function ServiceFailed(result){ alert('Service call failed:'+ result.status +''+ result.statusText); varType = null; varUrl = null; varData = null; varContentType = null; varDataType = null; varProcessData = null;然而,ServiceFailed函数调用消息服务调用失败0错误

$ b

如何进行跨域调用WCF服务?(使用jQuery或不使用)

感谢

解决方案

基本上你需要使用jSONP而不是jSON:

使用jQuery& JSONP用于具有WCF服务的跨网域AJAX

如果此链接消失,我会立即提供摘要。

有关如何使用jSONP,jQuery的更多信息,另请参阅 jQuery文档 & AJAX

I have a WCF service, this is a method which I'd to call:

[OperationContract] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)] double?[] GetPoints(string tourname);

I checked by WCF Test Client, it works fine

So I need to call this method from html page. It should work from other computer that is cross-domain.

I wrote somethig using jQuery 1-6-2.min.js:

var varType; var varUrl; var varData; var varContentType; var varDataType; var varProcessData; function CallService() { alert("CallService"); $.ajax({ type : varType, //GET or POST or PUT or DELETE verb url : varUrl, // Location of the service data : varData, //Data sent to server contentType : varContentType, // content type sent to server dataType : varDataType, //Expected data format from server processdata : varProcessData, //True or False success : function(msg) {//On Successfull service call ServiceSucceeded(msg); }, error: ServiceFailed// When Service call fails }); } function Start() { varType = "POST"; varUrl = "localhost:1592/TourService.svc/GetPoints"; varData = '{"tourname ":"customname"}'; varContentType = "application/json; charset=utf-8"; varDataType = "json"; varProcessData = true; CallService(); } function ServiceSucceeded(result) { alert("ServiceSucceeded"); alert(result); } function ServiceFailed(result) { alert('Service call failed: ' + result.status + ' ' + result.statusText); varType = null; varUrl = null; varData = null; varContentType = null; varDataType = null; varProcessData = null; }

However ServiceFailed function is invoked with message "Service call failed 0 error"

How to make a cross-domain calling of WCF service?(using jQuery or not)

Thanks

解决方案

Basically you need to be using jSONP instead of jSON:

Using jQuery & JSONP for cross-domain AJAX with WCF services

I'll provide a summary shortly in case this link disappears.

Also see the jQuery documentation for more about how to use jSONP, jQuery & AJAX

更多推荐

跨域调用WCF服务

本文发布于:2023-11-28 12:47:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642462.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:WCF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!