【问题标题】:JQUERY AJAX WITH WCF REST BASED RESPONSE IN JSONJSON 中基于 WCF REST 的响应的 JQUERY AJAX
【发布时间】:2013-06-21 19:35:42
【问题描述】:

我创建了一个基于 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 代码:

IService1:

   [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();" />

请帮帮我。

【问题讨论】:

    标签: json jquery wcf-rest


    【解决方案1】:

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

    This 的回答可能会对您有所帮助。

    【讨论】:

    • 您好,但我在 IIS 的同一台机器上托管了我的 wcf 服务和 wcf 客户端,因为它们都是用 asp.net C# 编写的。您能简要介绍一下外行风格的跨域吗? crossDomainScriptAccessEnabled="true" 属性不能在 VS2008 中添加 .net framework 3.5 webconfig。你能帮我解决这个问题吗
    猜你喜欢
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 2012-03-20
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    相关资源
    最近更新 更多