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
Solved

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

Posted on 2012-03-28
2
589 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
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

839 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