Solved

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

Posted on 2012-03-28
2
602 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Accepted Solution

by:
Steve Krile earned 500 total points
ID: 37781235
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
ID: 37782055
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 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