How to return JSON with ASP NET jQuery

0 votes

how I can return JSON data with my code.

JS

$(function () {
$.ajax({
        type: "POST",
        url: "Default.aspx/GetProducts",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            // How to return data here like a table???  
            $("#Second").text(msg.d);
            //alert(msg.d);
        }
    }); 
});

C# of Default.aspx.cs

[WebMethod]
public static string GetProducts()
{
   var products  = context.GetProducts().ToList();   
   return What do I have to return ????
}

Sep 9, 2020 in Java-Script by kartik
• 37,520 points
2,527 views

1 answer to this question.

0 votes

Hello @kartik,

You need to do something like this:

[WebMethod]
public static string GetProducts()
{
  // instantiate a serializer
  JavaScriptSerializer TheSerializer = new JavaScriptSerializer();

  //optional: you can create your own custom converter
  TheSerializer.RegisterConverters(new JavaScriptConverter[] {new MyCustomJson()});

  var products = context.GetProducts().ToList();   

  var TheJson = TheSerializer.Serialize(products);

  return TheJson;
}

You can reduce this code further but I left it like that for clarity. In fact, you could even write this:

return context.GetProducts().ToList();

and this would return a json string. I prefer to be more explicit because I use custom converters. There's also Json.net but the framework's JavaScriptSerializer works just fine out of the box.

Hope it helps!!
Thank You!!

answered Sep 9, 2020 by Niroj
• 82,840 points

Related Questions In Java-Script

0 votes
1 answer

How to send data in request body with a GET when using jQuery $.ajax()?

Hello @kartik, Sending the data in your scenario,I ...READ MORE

answered Jun 18, 2020 in Java-Script by Niroj
• 82,840 points
19,492 views
0 votes
1 answer

How to change an element's ID with jQuery?

Hello @kartik, Your syntax is incorrect, you should ...READ MORE

answered Aug 25, 2020 in Java-Script by Niroj
• 82,840 points
3,927 views
0 votes
1 answer

How to set “checked” for a checkbox with jQuery?

Hello @kartik, Use: $(".myCheckbox").attr('checked', true); // Deprecated $(".myCheckbox").prop('checked', true); And if ...READ MORE

answered Aug 28, 2020 in Java-Script by Niroj
• 82,840 points
6,196 views
0 votes
1 answer

How to Change the selected value of a drop-down list with jQuery?

Hello @kartik, With hidden field you need to ...READ MORE

answered Sep 9, 2020 in Java-Script by Niroj
• 82,840 points
2,843 views
0 votes
1 answer

Why it is necessary to refresh CSRF token per form request?

Hello, Generating a new CSRF token for each ...READ MORE

answered Mar 19, 2020 in Laravel by Niroj
• 82,840 points
4,553 views
0 votes
1 answer

What is meant by passing the variable by value and reference in PHP?

Hello, When the variable is passed as value ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,840 points
3,255 views
0 votes
1 answer

Connection with MySQL server using PHP. How can we do that?

Hey @kartik, You have to provide MySQL hostname, ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,840 points
1,289 views
0 votes
1 answer

How to retrieve or obtain data from the MySQL database using PHP?

Hello kartik,  Actually there are many functions that  ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,840 points
3,350 views
0 votes
1 answer

How to call jQuery UI Dialog with ASP.NET button postback?

Hello @kartik, To solve the problem with ASP:Button ...READ MORE

answered Apr 28, 2020 in Java-Script by Niroj
• 82,840 points
3,631 views
0 votes
1 answer

How to set outerHTML with jQuery?

hello @kartik, This is innterHTML because it's inside ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,840 points
2,362 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP