0

我创建了一个基于 REST 的 WCF,其中 ResponseFormat 位于 Json 中。我使用网页中的 jquery ajax 调用此 wcf。一切正常。我将 IIS 7.5 中的 wcf 服务部署为端口 8014 中的一个单独网站。我部署了 wcf 调用客户端,即包含 wcf 的 jquery ajax 调用的页面作为端口 8018 中的一个单独网站。现在当我尝试访问基于 Rest 的 wcf我收到错误为“服务调用失败:0”。我使用 VS2008 Framwork 3.5 进行开发。

WCFREST 代码:

服务1:

   [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetProvinceREST/{Country}",
            BodyStyle = WebMessageBodyStyle.Bare)]
        string[] GetProvinceREST(string Country);

服务1:

 public string[] GetProvinceREST(string Country)
        {
            string[] str = new string[3];
            str[0]= "hi";
            str[1]= "how";
            str[2]= "are";
            return str;
        }

Jquery Ajax 调用代码:

 function CountryProvinceWCFREST() {debugger;
            varType = "GET";
            varUrl = "http://localhost:8014/Service1.svc/GetProvinceREST/" + $('#ddlCountry').val();
            varContentType = "application/json; charset=utf-8";
            varDataType = "json";
            varProcessData = false;
            CallService();
        }


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


var ProvinceDDL = document.getElementById("ddlProvince");
          for (j = ProvinceDDL.options.length - 1; j >= 0; j--) { ProvinceDDL.remove(j); }
            var resultObject = null;
if (varType == "GET") { resultObject = result; }
 for (i = 0; i < resultObject.length; i++) {
                    var opt = document.createElement("option"); opt.text = resultObject[i];
                    ProvinceDDL.options.add(opt)
                }

  <input type="button" value="Invoke" id="Button2" onclick="CountryProvinceWCFREST();" />

请帮帮我。

4

1 回答 1

0

这是由于跨域调用,这意味着托管的服务位于某个不同的域上。您可能想要使用 JSONP。

这个答案可能会进一步帮助你。

于 2013-06-25T05:57:13.737 回答