1

I have a simple javascript object. I serialize it with JSON.stringify I send it to a asp.net web function that just return it. But when I try to parse the returned string with JSON i get

Microsoft JScript runtime error: Invalid character

 $(document).ready(function() {
          $.ajax({
              type: "POST",
              url: "test.aspx/PassBackdata",
              contentType: "application/json; charset=utf-8",
              data: "{'args': '" + JSON.stringify(MyObject) + "'}",
                   dataType: "json",
                   success: AjaxSucceeded,
                   error: AjaxFailed
               }); 
           });
           function AjaxSucceeded(result) {

            var a=JSON.parse(result);

           }
           function AjaxFailed(result) {
               alert(result.status + ' ' + result.statusText);
           }  


      };




  <System.Web.Services.WebMethod()> _
    Public Shared Function PassBackdata(args As String)
            Return args
     End Function

How can I solve this problem? Thank you

4 Answers 4

3

If the error occurs on succes function, you may want to check the format of the result object. I had to use var a=JSON.parse(result.d); because that is how it was returned by webservice, it wasn't a direct json, but an object with a "d" field which was the json.

For checking the result I use fiddler.

Sign up to request clarification or add additional context in comments.

1 Comment

I experienced it too! Weird!
0

Instead of:

"{'args': '" + JSON.stringify(MyObject) + "'}"

Try this:

JSON.stringify({args: MyObject})

Don't do yourself what JavaScript can do for you ;)

Comments

0

It would help to know what MyObject looks like, however:

JSON must have key names in double quotes, not single quotes. Try something like this instead:

$(document).ready(function() {
          $.ajax({
              type: "POST",
              url: "test.aspx/PassBackdata",
              contentType: "application/json; charset=utf-8",
              data: JSON.stringify({args:MyObject}),
                   dataType: "json",
                   success: AjaxSucceeded,
                   error: AjaxFailed
               }); 
           });
           function AjaxSucceeded(result) {

            var a=JSON.parse(result);

           }
           function AjaxFailed(result) {
               alert(result.status + ' ' + result.statusText);
           }  


      };

2 Comments

with data: JSON.stringify({args:MyObject}) i get an error 500 error internal server. The strange thing is that the string I send is exactly the same as what i receive. I do not understand the invalid character error message.
with data: JSON.stringify({args:MyObject}) i get an error 500 error internal server. The strange thing is that the string I send is exactly the same as what i receive. I do not understand the invalid character error message. If I stringify and parse immediatly all work whell. If i send back the string to an asp.net function and the function sends it back as it is the received string can not be parsed with JSON even if it looks identical may be it is related with utf-8, does something to change it
0

if I do: JSON.parse(result.d) instead of JSON.parse(result) it works.

function AjaxSucceeded(result) {

    var a=JSON.parse(result.d);

}

don't know why

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.