Solved

How to handle $.getJSON() or $.post in ASP.net 4.0 Code behind?

Posted on 2012-03-28
2
585 Views
Last Modified: 2012-04-01
Hi,
     I have worked in MVC .So i Know how this works in MVC .But currently i am planning to use Knockout in ASP.NET 4.0 web app.

So how to handle Ajax request in asp.net .Please provide me with some good examples.
Thanks in advance.
0
Comment
Question by:hariaspind
2 Comments
 
LVL 16

Accepted Solution

by:
Steve Krile earned 500 total points
Comment Utility
Create an .asmx file.  The file should look something like this:

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Web.Script.Serialization
Imports System.Web.Script.Services

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class MyWebService
    Inherits System.Web.Services.WebService
    
     'this is the database context for my project (I am using Linq to SQL in this example)
     Dim db As New EnvironmentWebModelDataContext()
    <WebMethod()> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function DoSomething() As String
        'I use Entity framework or Linq to SQL here to talk to my DB and get a
        'generic list of something
        dim result = From p as Person in db.People where p.LastName = "Jones"

        Return New JavaScriptSerializer().Serialize(result)
    End Function
End Class

Open in new window



You can then access this information in a jquery ajax call like this:

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "url to your .asmx file",
        data: JSON.stringify(<any data you have for parameters of the service>),
        dataType: "json",
        success: function (data, textResponse) {
            result = $.parseJSON(data.d);
        }
    });

Open in new window



That's pretty much it.  Very simple.  Here are the keys:

1. Use asp.net's Imports System.Web.Script.Serialization to serialize your generic objects.  Except for dates, it does a great job.
2.  Make sure to have this in your web.config:
<system.web>
    <webServices>
      <protocols>
        <add name="HttpGet" />
        <add name="HttpPost" />
      </protocols>
    </webServices>
</system.web>

Open in new window

3.  Finally, a watchout-out.  I have found expanding the size of request that is permitted has fixed some issues with queries that result in a lot of data.
  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="5000000" />
      </webServices>
    </scripting>
  </system.web.extensions>

Open in new window

0
 
LVL 14

Expert Comment

by:binaryevo
Comment Utility
This is a decent example:  http://www.codeproject.com/Articles/332406/Client-side-Model-binding-with-ASP-NET-MVC-3-and-K

Look at the code tab itself and look MarketWatchController and MarketWatchPriceUI.

Hope this helps!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now