I have this following ajax to call my service which fetches some records from the Database and fills it in my frontend...but i get the json:unexpected character error...can u please help me in solving this ...
'http://localhost/WcfService/Service1.svc/remarksList';
var tempyear="";
$.ajax({
url: urlToHandler,
data:JSON.stringify({oei:{"reqNo1":reqNo,"loginid":userid}}),
type: 'POST',
dataType:"json",
contentType: 'application/json',
success: function(data) {
alert(data.fillRemarksListResult);
myData = JSON.parse(data.fillRemarksListResult, function (key, value) {
var type;
if (value && typeof value === 'object') {
type = value.type;
if (typeof type === 'string' && typeof window[type] === 'function') {
return new (window[type])(value);
}
}
return value;
});
alert(myData);
$.each(data.fillRemarksListResult,function(key,val){
alert(val.rmrkreqNo1);
});
},
error: function(data, status, jqXHR) {
alert('There was an error.');
}
}); // end $.ajax
});
Now i get this error :
*Json.parse:unexpected character myData=JSON.parse(data.fillRemarksListResult, function (key, value) {*
ALSO MY AJAX CALL RETURNS [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object] when given as alert...
1 Answer 1
There are 2 ways to resolve this
Way 1]
Remove dataType:"json", from your $.ajax call.
Ans use JSON.parse().
Way 2]
Keep dataType:"json", as it is and DO NOT use JSON.parse()
WHY WHY WHY
Here are the reasons.
As per $.ajax() API docs, when you specify dataType:'json' property, the jQuery will automatically parse the response from the server and pass the parsed JSON object to your success callback.
In this case, you are reparseing the JSON object, and that is why the error is thrown.
So either do not specify dataType:"json" and parse the returned string
OR
Specify dataType:"json" and do not parse it.