1

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...

asked Jan 29, 2013 at 11:51

1 Answer 1

10

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.

answered Jan 29, 2013 at 12:02
Sign up to request clarification or add additional context in comments.

1 Comment

Thank u so much for the crystal clear explanation dude...For a newbie lyk me dis was very helpful..!!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.