语法错误:jQuery $ .ajax调用中的字符无效在哪里?

我有以下一点jQuery调用WCF方法。 方法调用成功到我可以看到它记录的程度,它确实返回一个布尔值true。 但是,error handling程序将返回“CallIsDataReady中的AJAX调用失败”和“语法错误:无效字符”。 然后它不会将成功路径调用callUpdateGrid。 我找不到无效字符。 救命!

function CallIsDataReady(input) { $.ajax({ url: "http://www.blah.com/services/TestsService.svc/IsDataReady", type: "GET", contentType: "application/json; charset=utf-8", data: input, dataType: "json", success: function (data) { if (!data) { setTimeout(function (inputInner) { CallIsDataReady(inputInner); }, 1000); } else { console.log("data returned - calling callUpDateGrid"); //Continue as data is ready callUpdateGrid(input); } }, error: function (jqXHR, textStatus, errThrown) { console.log("AJAX call failed in CallIsDataReady"); console.log(errThrown); } }); } $(document).ready(function () { var input = { "requestGUID": "" }; CallIsDataReady(input); }); 

服务器端方法返回JSON,因为它是支持AJAX的Web服务:

 [OperationContract] [WebGet] public bool IsDataReady(string requestGUID) { bool isReady = Global.publicDataDictionary.Keys.Contains(requestGUID); using (savitasEntities2 db = new savitasEntities2()) { DataRequestLog drl = new DataRequestLog(); drl.registrationID = ""; drl.request = "Is Ready=" + isReady; drl.connectionID = ""; drl.created = System.DateTime.Now.ToUniversalTime(); drl.direction = "tickler"; drl.dataRequestGUID = requestGUID; db.DataRequestLogs.Add(drl); db.SaveChanges(); } return isReady; } 

编辑:第二个JavaScript方法是:

  function callUpdateGrid(input) { console.log(input); $.ajax({ url: "http://www.blah.com/services/TestsService.svc/GetContactsDataAndCountbyGUID", type: "GET", contentType: "application/json; charset=utf-8", data: input, dataType: "json", success: function (data) { var mtv = $find("").get_masterTableView(); console.log(data); mtv.set_dataSource(data.d.Data); mtv.dataBind(); }, error: function (jqXHR, textStatus, errThrown) { console.log("AJAX call failed in callUpdateGrid"); console.log(errThrown); } }); } 

由于您的代码已经运行但仍然需要考虑这些因素。 我不是JavaScript的专家,但我相信你调用方法的方式是不正确的。 首先,您的OperationContractWebGet ,因此您无需在请求中提供Content-Type 。 当您使用POST方法发送大量数据时,通常会使用Content-Type 。 所以,据我所知,改变如下:

 [OperationContract] [WebGet(UriTemplate = "/GUID={requestGUID}", ResponseFormat = WebMessageFormat.Json)] public bool IsDataReady(string requestGUID) { bool isReady = Global.publicDataDictionary.Keys.Contains(requestGUID); using (savitasEntities2 db = new savitasEntities2()) { DataRequestLog drl = new DataRequestLog(); drl.registrationID = ""; drl.request = "Is Ready=" + isReady; drl.connectionID = ""; drl.created = System.DateTime.Now.ToUniversalTime(); drl.direction = "tickler"; drl.dataRequestGUID = requestGUID; db.DataRequestLogs.Add(drl); db.SaveChanges(); } return isReady; } 

我希望这肯定会奏效。 现在这个方法将以JSON格式返回isReady 。 如果未在OperationContract指定ResponseFormat ,则默认情况下它将返回到XML

现在尝试删除Content-Type: application/json; charset=utf-8 Content-Type: application/json; charset=utf-8因为您没有以JSON格式发送数据并执行您的方法。 响应将采用JSON格式,因此,您必须解析它以获取isReady值。

使用此URL来呼叫服务:

 url: "http://www.blah.com/services/TestsService.svc/GUID=abc123" 

这很简单,易于实现。 谢谢你的耐心。

编辑:BodyStyle = WebMessageBodyStyle.Bare中添加BodyStyle = WebMessageBodyStyle.Bare OperatcionContract