'Syntax Error: Invalid character in jQuery $.ajax call where?
I have the following bit of jQuery calling a WCF method. The method call succeeds to the extent that I can see it logging and it does return a Boolean true. However, the error handler is coming back with "AJAX call failed in CallIsDataReady" and "Syntax Error: Invalid character." It then does not take the success path call the callUpdateGrid. I can't find the Invalid character. Help!
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": "<%=guid %>" };
CallIsDataReady(input);
});
Server side method returns JSON as it is an AJAX enabled Web service:
[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;
}
EDIT: the 2nd JavaScript method is:
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("<%= RadGrid1.ClientID %>").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);
}
});
}
Solution 1:[1]
As you have got your code working but still you should consider these things in mind. I am not expert in JavaScript but I believe that the way you are calling method is incorrect. First of all, your OperationContract
is WebGet
, so you don't need to provide Content-Type
in your request. Content-Type
is normally used when you are sending large amount of data using POST method. So, according to me, change following:
[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;
}
I hope that this will definitely work. Now this method will return isReady
in JSON format. If you do not specify ResponseFormat
in your OperationContract
, then by default it will be returned into XML
.
Now try removing Content-Type: application/json; charset=utf-8
as you are not sending data in JSON and execute your method. Response will be in JSON format, so, you have to parse it to get the isReady
value.
Use this URL to call service:
url: "http://www.blah.com/services/TestsService.svc/GUID=abc123"
This is simple and easily implementable. Thanks for your patience.
EDIT: Add BodyStyle = WebMessageBodyStyle.Bare
in your OperatcionContract
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |