對於我最近的項目,我創建了一個Web服務,它將自定義類型的數組返回給jquery客戶端代碼。 WCF通過$.ajax
命令調用,並位於同一個域中。WCF jquery parsererror未終止的字符串常量響應
當我在localhost上運行我的web應用程序(這是在本地機器上運行的IIS)時,一切正常。當我將它部署到我們的集成服務器時,突然ajax調用WCF結束時出現錯誤:「parsererror - 未終止的字符串常量」,狀態爲200.然而,返回的消息類似於"[{\"Text\":\"Test dodatnih naslov"
,這當然不是正確的json格式。
正確反應應該是:"[{"Text":"Test dodatnih naslovov","Value":"100"},{"Text":"Test dodatnih naslovov - ISO2","Value":"101"},{"Text":"UPDATE","Value":"102"}]"
我已經追蹤WCF服務malfuncitons,但它似乎並沒有被撞毀。我也試過並設置了超時給ajax調用,但無濟於事。一些幫助將不勝感激。
我的IIS是IIS7,其中積分IIS6運行在Windows Server 2008上
JS文件
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的部分
<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>