Skip to content Skip to sidebar Skip to footer

MVC: While Retrieving The Passed Json Object From Controller In View, JavaScript Error

I'm passing some string messages as Json object in view. public ActionResult SomeAction(someObject object) { ..... ..... if (check1) { retu

Solution 1:

If you execute the following line of code

var a = { success : "success" };
var b = JSON.parse(a);

you will get the error you have mentioned about "SyntaxError: Unexpected token o..."

Don't know why are you trying to convert your already JSON object using JSON.parse(), instead you could use this

a.success

to read the "success" value .


Solution 2:

the variable type is detected as json object.

var x = {"error":"somemessage"};
alert(x.error)

The variable is detected as String here.

var x = JSON.parse('{"error":"somemessage"}'); alert(x.error)

If you notice, the difference is #1 starts with {(curly braces) whereas #2 starts with '(apostrophe)


Solution 3:

Try not parsing the answer (so remove "JSON.parse(data)" line) and read everything directly from "data" variable.


Solution 4:

You have to use data.sucess to get your result . There is no need to parse again.


Post a Comment for "MVC: While Retrieving The Passed Json Object From Controller In View, JavaScript Error"