user2864496
user2864496

Reputation: 107

Json Parsing error when accessing in jquery of a variable which is serialized in asp.net .cs page

Hi I'm exucuting the following code and I kept the following code in separate js file and referencing that file in all other .aspx files. When I put following code in .aspx file script section directly it's working properly but when I kept it in separate js file along with other functions it's not working properly

function pageLoad(sender, args) {

  var dict = $.parseJSON('<%=DictJson %>');
var data = eval(dict);

}

I'm getting error at var dict = $.parseJSON('<%=DictJson %>');. The error being displayed is Microsoft JScript runtime error: Exception thrown and not caught and it is displaying that throw in json file } if (!v("json-parse")) { var M = String.fromCharCode, N = { 92: "\", 34: '"', 47: "/", 98: "\u0008", 116: "\t", 110: "\n", 102: "\u000c", 114: "\r" }, b, A, j = function () { b = A = w; throw SyntaxError(); }, q = function () { for (var a = A, f = a.length, c, d, h, k, e; b < f; ) { e = a.charCodeAt(b); switch (e) { case 9: case 10: case 13: case 32: b++;

I wrote the DictJson is a parsed dictionary I'm parsing it in back end in some other base page where it can be accessed by each and every page. The parsing code is

    public string DictJson
    {
        get
        {

            MouseOverFieldDict mdict = (MouseOverFieldDict)(HttpContext.Current.Session["MouseOverFieldDict"]);
            JavaScriptSerializer jSer = new JavaScriptSerializer();
            return jSer.Serialize(mdict.FieldDict);

        }
    }

Please help me with the error. I don't get it why it is throwing that error in script

Upvotes: 0

Views: 227

Answers (2)

Ryan Anderson
Ryan Anderson

Reputation: 154

The server-side expression "<%=DictJson %>" is only evaluated in the .aspx file if DictJson is an accessible member (property/field) in the code-behind. The request for the JavaScript file is a completely separate request, so it is not aware of DictJson.

You can still have your function in your JavaScript, but I would suggest something like this:

JavaScript file:

function myPageLoad(DictJson) {
  var dict = $.parseJSON(DictJson);
  var data = eval(dict);
}

Code Behind (roughly, I'm not set up for c#):

void Page_PreRender(object sender, EventArgs args) {
  string scriptName;
  string scriptText;

  // have jQuery to call the function in your JS with the serialized Dict
  scriptText = "$(function(){myPageLoad('" + DictJson + "');});";
  scriptName = "onLoadScript";

  ClientScriptManager.RegisterStartupScript(Me.GetType(), scriptName, scriptText, True);
}

RegisterStartupScript documentation: http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx

Upvotes: 1

mreyeros
mreyeros

Reputation: 4379

If I am not mistaken, the issue is that when you move the javascript into its own separate file, javascript does not know how to natively handle the "gator" tags (<%=...%>). You would either need to leave your script inline in your aspx page or would need to come up with a mechanism to pass into your function call the value of DictJson when you make the function call.

Upvotes: 0

Related Questions