WCF jquery parsererror未终止字符串常量响应

时间:2013-05-09 10:59:51

标签: wcf

对于我最近的项目,我创建了一个Web服务,它将一个自定义类型的数组返回给jquery客户端代码。 WCF由$.ajax命令调用,并且位于同一域中。

当我在localhost(在本地计算机上运行IIS)上运行我的Web应用程序时,一切正常。当我将它部署到我们的集成服务器时,突然对WCF的ajax调用结束时出现错误:“parsererror - unterminated string constant”,状态为200.返回的消息有点像"[{\"Text\":\"Test dodatnih naslov",当然这不是正确的json格式。

应该做出正确的回应:"[{"Text":"Test dodatnih naslovov","Value":"100"},{"Text":"Test dodatnih naslovov - ISO2","Value":"101"},{"Text":"UPDATE","Value":"102"}]"

我已经跟踪WCf服务的故障,但它似乎没有崩溃。我也试过并设置超时到ajax调用,但无济于事。一些帮助将非常感激。

我的IIS是IIS7,其中集成在Windows Server 2008上运行IIS6。

js file

function InsuranceClientContact_ItemsRequesting(o, e) {
    var $ = $telerik.$;
    var urlSvc = ServiceBaseUrl + '/GetContacts'
    $.ajax({
        type: "POST",
        url: urlSvc,
        data: '{"ixClient": ' + selectedItemId + '}', //selectedItemId is a positive number
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            // do something
        },
        error: function (result) {
            var msg = result.status + " - " + result.statusText;
            setTimeout(function () { throw new Error(msg) }, 0);
        }
    });

wcf界面

namespace Sid.Skode.Web.Services.Populate {
    [ServiceContract]
    public interface IInsuranceClientContactService {
        [OperationContract]
        [WebInvoke(Method="POST",
            BodyStyle=WebMessageBodyStyle.WrappedRequest,
            ResponseFormat=WebMessageFormat.Json)]
        Contact[] GetContacts(long ixClient);
    }

    [DataContract]
    public class Contact {
        [DataMember]
        public string Text;
        [DataMember]
        public string Value;
    }

}

wcf服务实现

namespace Sid.Skode.Web.Services.Populate {
    [AspNetCompatibilityRequirements( RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed )]
    public class InsuranceClientContactService : IInsuranceClientContactService {

        public Contact[] GetContacts( long ixClient ) {
            return GetContactsFromDatabase( ixClient );
        }

        #region Private methods

        private Contact[] GetContactsFromDatabase( long ixClient ) {
            DataTable dt = GetDataFromDataBaseById( ixClient );
            return ConvertDataTableToContactArray( dt );
        }        

        private DataTable GetDataFromDataBaseById( long ixClient ) {
            AutoCompleteBLL model = new AutoCompleteBLL();
            return model.SearchContactsByPartner( ixClient );
        }

        private Contact[] ConvertDataTableToContactArray( DataTable dt ) {
            Contact[] rgContact = new Contact[dt.Rows.Count];
            int cnContact = 0;
            foreach (DataRow dr in dt.Rows) {
                if (!dr.IsNull( "NAZIV" )) {
                    Contact contact = new Contact();
                    contact.Text = dr["NAZIV"].ToString();
                    contact.Value = dr["ID_DODATEN_KONTAKT"].ToString();
                    rgContact[cnContact++] = contact;
                }
            }
            return rgContact;
        }

        #endregion

    }
}

web.config wcf part

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="httpServiceBehavior">
          <serviceMetadata httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="httpEndpointBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBindingWithTransportWindowsSecurity">
          <security mode="Transport">
            <transport clientCredentialType="Windows" />
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="false" aspNetCompatibilityEnabled="true" />
    <services>
      <service name="Sid.Skode.Web.Services.Populate.InsuranceClientContactService" behaviorConfiguration="httpServiceBehavior">
        <endpoint address="" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithTransportWindowsSecurity"
                  contract="Sid.Skode.Web.Services.Populate.IInsuranceClientContactService"
                  behaviorConfiguration="httpEndpointBehavior">
        </endpoint>
        <endpoint
          address="mex"
          binding="mexHttpsBinding"
          bindingConfiguration=""
          contract="IMetadataExchange"/>
      </service>
    </services>
  </system.serviceModel>

1 个答案:

答案 0 :(得分:0)

如上所述here,您需要从Web配置中删除RadCompression http模块的所有实例。然后,它有效。