Encountered unexpected character

WCF Encountered unexpected character

Do you encounter above issue while integrating WCF with ajax /jQuery

image

reasons can be many but if you encounter this when passing some parameters to your function, go ahead and read it.

When i was calling the service as below i was getting above error (above trace i took from fiddler)

$.ajax({                 
type: "POST",                 
url: serviceUrl + "GetTask",                 
dataType: "json",                 
data: { id: 16 },
contentType: "application/json; charset=utf-8",                 
success: function (json) {                     
var output = $('#MyTemplate').parseTemplate(json);                     
$('#MyTemplateOutput').html(output);                 
}});
the issue here was how i pass data to the server.
 
WCF wants to see the name of the properties in the JSON wrapped with ""
and once i converted above string call to
 
$.ajax({                 
type: "POST",                 
url: serviceUrl + "GetTask",                 
dataType: "json",                 
data: '{"id": #}'.replace('#', urlString),                 
contentType: "application/json; charset=utf-8",                 
success: function (json) {                     
var output = $('#MyTemplate').parseTemplate(json);                     
$('#MyTemplateOutput').html(output);                 
}});

 

SEE how i have changed to data being sent to the server.

i used # as a place holder and replace it with urlString which is my parameter from url.

If above helped you too fixing this issue. Lets us know.

Advertisements

One thought on “Encountered unexpected character

  1. one useful thing i just noticed regarding above is
    previously it was sending
    id: 16
    to the server which ofcourse not in JSON format.

    and after my fix it sends {“id”: 16} which is real JSON.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s