Solved

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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

713 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