Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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 article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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 …

751 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