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

  • SEARCH
  • Home
  • 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 1050797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:49:29+00:00 2026-05-16T16:49:29+00:00

I have an Ajax request: $.ajax({ url: MyPage.aspx, data: params, success: function(data) { //

  • 0

I have an Ajax request:

$.ajax({ url: "MyPage.aspx", 
    data: params,
    success: function(data) {
        // Check results                
        $('#testp').append(data.message);
        enableForm();
    },
    error: function() {
        alert('Unable to load the permissions for this user level.\n\nYour login may have expired.');
        enableForm();
    },
    dataType: "json"
});

On the request page there is C# code that does this at the end of Page_Load:

Response.AppendHeader("X-JSON", result);

‘result’ is formatted like this:

{ "success": true, "message": "SUCCESS", "user_level": 25, "switches": [ { "number": 30, "is_enabled": false, "is_default": false }, { "number": 30, "is_enabled": false, "is_default": false } ]}

The request returns successfully, but ‘data’ is null. What am I missing?

Thanks.

  • 1 1 Answer
  • 2 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-16T16:49:30+00:00Added an answer on May 16, 2026 at 4:49 pm

    Your main problem seems to be that you’re returning the JSON data in an HTTP header instead of as the content of the response. You probably want to do something like this:

    Response.ContentType = "application/json";
    Response.Write(result);
    Response.End();
    

    That might fix your immediate problem, but I would strongly recommend that you avoid the approach of using an ASPX page’s direct ouput. There’s a lot of unnecessary overhead involved in getting to the point of Page_Load, when all you really want is a simple JSON endpoint. Not to mention, manually handling the JSON serialization isn’t necessary.

    If you’re building that JSON string from an object on the server-side, you can use an ASP.NET AJAX “Page Method” to return that directly and let the framework handle serialization. Like this:

    public class PermissionsResult
    {
      public bool success;
      public string message;
      public int user_level;
    
      public List<Switch> switches;
    }
    
    public class Switch
    {
      public int number;
      public bool is_enabled;
      public bool is_default;
    }
    
    // The combination of a WebMethod attribute and public-static declaration
    //  causes the framework to create a lightweight endpoint for this method that
    //  exists outside of the normal Page lifecycle for the ASPX page.
    [WebMethod]
    public static PermissionsResult GetPermissions(int UserLevel)
    {
      PermissionsResult result = new PermissionsResult();
    
      // Your current business logic to populate this permissions data.
      result = YourBusinessLogic.GetPermissionsByLevel(UserLevel);
    
      // The framework will automatically JSON serialize this for you.
      return result;
    }
    

    You’ll have to fit that to your own server-side data structures, but hopefully you get the idea. If you already have existing classes that you can populate with the data you need, you can use those instead of creating new ones for the transfer.

    To call an ASP.NET AJAX Page Method with jQuery, you need to specify a couple extra parameters on the $.ajax() call:

    $.ajax({
      // These first two parameters are required by the framework.
      type: 'POST',
      contentType: 'application/json',
      // This is less important. It tells jQuery how to interpret the
      //  response. Later versions of jQuery usually detect this anyway.
      dataType: 'json',
      url: 'MyPage.aspx/GetPermissions',
      // The data parameter needs to be a JSON string. In older browsers,
      //  use json2.js to add JSON.stringify() to them.
      data: JSON.stringify({ UserLevel: 1}),
      // Alternatively, you could build the string by hand. It's messy and
      //  error-prone though:
      data: "{'UserLevel':" + $('#UserLevel').val() + "}",
      success: function(data) {
        // The result comes back wrapped in a top-level .d object, 
        //  for security reasons (see below for link).
        $('#testp').append(data.d.message);
      }
    });
    

    Regarding the data parameter, here is info on it needing to be a string: http://encosia.com/2010/05/31/asmx-scriptservice-mistake-invalid-json-primitive/

    Also, here is more on using the JSON.stringify() approach: http://encosia.com/2009/04/07/using-complex-types-to-make-calling-services-less-complex/

    The .d issue is one that can be confusing at first. Basically, the JSON will come back like this instead of how you might expect:

    {"d": { "success": true, "message": "SUCCESS", "user_level": 25, "switches": [ { "number": 30, "is_enabled": false, "is_default": false }, { "number": 30, "is_enabled": false, "is_default": false } ]}}
    

    It’s easy to account for once you expect it. It makes your endpoint more secure by mitigating against a fairly treacherous client-side exploit when the top level container is an array. Not applicable in this specific case, but nice to have as a rule. You read more about that here: http://encosia.com/2009/02/10/a-breaking-change-between-versions-of-aspnet-ajax/

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

Sidebar

Related Questions

I have ajax request result = $.ajax({ url: '/live-sell-search.php?id=123', success: function(result) { $('.results-list').html(result); }
I have a very simple ajax request: $.get(url, data) .done(function () { }) .fail(function
I have an an ajax request that looks like this, $('input.fakecheck').click(function(){ alert(deleteing....); $.ajax({ url:/search,
I have an ajax request sending some data to a php file: Ext.Ajax.request({ url:
I have a problem with using Ajax. function GetGrantAmazonItemCnt(){ var cnt; Ext.Ajax.request({ url :
Ext.Ajax.request({url:'DeleteAction',success: doneFunction,failure: errorFunction,params:{name:rname}}); The above code is my Ajax request which goes to DeleteAction
I have the following code Ext.Ajax.request({ url: 'newRecord.php', method: 'POST', type: 'json', params: {
I have jQuery.ajax() creating a request to a url (cms2/docman/dir/%id) (%id is a numeric
I have made my own custon vtype which performs an ajax request to check
I have an ajax request that looks like this, $(#frmProducts).submit(function(){ var dataSet = $(#frmProducts).serialize();

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.