跨域调用WCF服务
Cross-domain calling a WCF Service
我有一个WCF服务,这是一个我要调用的方法:
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
double?[] GetPoints(string tourname);
我通过WCF测试客户端检查,它工作正常
所以我需要从html页面调用这个方法。它应该可以在跨域的其他计算机上工作。
我用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 = "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函数被调用,消息"Service call failed 0 error"
如何跨域调用WCF服务?(是否使用jQuery)
谢谢
基本上你需要使用jSONP而不是jSON:
使用jQuery &使用WCF服务的跨域AJAX的JSONP
我将提供一个简短的摘要,以防这个链接消失。
关于如何使用jSONP、jQuery &AJAX
.net framework 4 for WCF现在内置了json回调,
我认为jquery1.5以后他们增加了以下选项,crossDomain,试试下面的代码
$.ajax({
type: "POST",
dataType: "html",
crossDomain: true,
url: "http://www.domain.com/page.aspx",
error: function() {
alert("error");
},
success: function(msg){
alert(msg );
}
});
相关文章:
- 如何使用WCF服务和javascript表单post上传.doc文件
- 从WCF服务接收应答's布尔方法
- 对wcf服务的javascript调用
- 使用Javascript连接到WCF Web服务
- 在javascript中验证wcf服务的正确端口
- 调用WCF JSONP web服务方法时出现错误400
- ajax调用wcf服务返回错误
- 将SQL查询+JSON文件生成移动到服务中?wcf?mvc
- 如何将用户从jquery移动窗体键入的查询SQL发布到WCF服务,并在WCF服务上运行该查询
- 将 json 发送到(不是 REST)WCF 服务
- Javascript 数组作为 WCF Web 服务参数
- jQuery.ajax 从 wcf 服务和显示中获取图像
- 如何在WCF服务中获取Xml作为字符串并使用JavaScript发送
- 通过 Javascript 向 Azure 上的 WCF Web 服务发出 SOAP 请求
- WCF Web 服务在通过 Javascript 调用时返回“错误请求”错误
- 从 JavaScript 调用 WCF 服务
- 在 WebApp 中配置启用 AJAX 的 WCF 服务以接收大型数据
- xml 在 WCF 休息服务响应中被更改 原因
- 访问HTTPS WCF服务,HTML,Javascript,服务和托管在不同域上的网页 - CORS
- 通过 ajax 调用 wcf 服务时出错