编程语言
首页 > 编程语言> > javascript-跨域调用WCF服务

javascript-跨域调用WCF服务

作者:互联网

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

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

我通过WCF测试客户端进行了检查,效果很好

所以我需要从html页面调用此方法.它应该可以在跨域的其他计算机上工作.

我使用jQuery 1-6-2.min.js编写了somethig:

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;
}

但是,通过消息“服务调用失败0错误”调用ServiceFailed函数

如何进行WCF服务的跨域调用?(是否使用jQuery)

谢谢

解决方法:

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

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

如果此链接消失了,我会尽快提供摘要.

另请参阅jQuery documentation,以了解有关如何使用jSONP,jQuery&的更多信息. AJAX

标签:cross-domain,web-services,wcf,javascript,jquery
来源: https://codeday.me/bug/20191208/2089412.html