Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3681550
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:42:03+00:00 2026-05-19T03:42:03+00:00

In the root of my .js file I declare: var Doe = { foo:

  • 0

In the root of my .js file I declare:

var Doe = {
            "foo": "The quick brown fox jumps over the lazy dog.",
            "bar": "ABCDEFG",
            "baz": [52, 97],
            "testfunc": function(){alert('json function');}
           }

And in my HTML:

<a id="lele" href="javascript:void(0)" onclick="Doe.testfunc()">Doe</a>

And when I click it it works, so I know it’s possible to put functions in JSON objets.

Now, in the root of the .js I declare:

$.getJSON("~/rule/2", function(data) {
    alert("2: " + data.foo);
    data.testfunc();
});

And the server is returning this for that url:

 context.Response.ContentType = "application/json";
 context.Response.Output.Write(@"{
                     ""foo"": ""The quick brown fox jumps over the lazy dog."",
                     ""bar"": ""ABCDEFG"",
                     ""baz"": [52, 97],
                     ""testfunc"": function(){alert('json function');}  
                                 }");
  context.Response.Flush();

But nothing happens 🙁 Aparently because if there is an error, .getJSON will fail silently. But, if I remove the lines regarding of the function:

$.getJSON("~/rule/2", function(data) {
    alert("2: " + data.foo);
});

and

 context.Response.Output.Write(@"{
                     ""foo"": ""The quick brown fox jumps over the lazy dog."",
                     ""bar"": ""ABCDEFG"",
                     ""baz"": [52, 97]}");

Then it works, an alert pop up with the content of the “foo” property.

What is the problem with the function? 🙁 In the moment that I put a function in the JSON object, jQuery fails.

Kind regards.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:42:03+00:00Added an answer on May 19, 2026 at 3:42 am

    You don’t have a JSON object.

    You have an object literal. Functions cannot be values of a JSON string.

    JSON is only a subset of the object literal syntax. Possible values are:

    values
    (source: json.org)

    Read more on json.org

    This is JavaScript object:

    var foo = {bar: 42}
    

    and this is a JSON string:

    var foo = '{"bar": 42}';
    

    See also JavaScript, JSON objects and object literal notation.

    Update: In order to return and execute JavaScript, you can do the following:

    You can return plain JavaScript e.g.

    context.Response.ContentType = "text/javascript";
    context.Response.Output.Write(@"
     function testfunc(){
         alert('json function')
     };
     testfunc();
    ");
    

    and use $.ajax with dataType: 'script':

    $.ajax({
        url: "~/rule/2",
        dataType: 'script',
        success: function(data) {}
    });
    

    DEMO

    You can also define JavaScript objects with functions like so:

    context.Response.ContentType = "text/javascript";
    context.Response.Output.Write(@"
         var obj = {
             bar: function(){alert(42);}
         }
         yourHandler(obj);
    ");
    

    You just need to have a global function called yourHandler (and of course still call ajax with dataType: script).

    This is probably what comes closest to what you want. The difference is, that you don’t return JSON, but JavaScript, and you don’t process the object in the success callback, but in another function, which has to be called from within the JS you sent.

    See another DEMO

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.