-2

I am using 3-tier architecture for demo application.

I am trying to call business logic layer method from presentation layer using Ajax. But It is showing error. I think there is some mistake in passing url.

Here is the Ajax call from Index.aspx page in the presentation layer:

$.ajax({
    type: "GET",
    url: "DemoApplication.BLL/BLL/ShowDetail",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: { },
    success: function (msg) {
        alert("data received");
    },
    error: function () {
        alert("couldn't proceed");
    }
});

Here is my method in business logic layer:

namespace DemoApplication.BLL
{
    public class BLL
    {
        public static List<User> ShowDetail()
        {
            DAL.DAL dal = new DAL.DAL();
            return dal.ShowDetail();
        }
    }
}
Celeo
  • 5,583
  • 8
  • 39
  • 41
Vivek
  • 363
  • 8
  • 25
  • That needs to be a web method you call, it can't be any server method. – tymeJV Jul 23 '15 at 20:30
  • 1
    The url isn't supposed to be the path to a class... It should be Index.aspx/TheMethod where TheMethod is a web method on Index.aspx. You can't just call any method in the world... That would be super frightening! – dmeglio Jul 23 '15 at 20:32
  • 2
    Decorate the function with `[WebMethod]` – mhu Jul 23 '15 at 20:33
  • @dman2306 method is present in the same solution as the presentation layer but in different projet – Vivek Jul 23 '15 at 20:35
  • @mhu I have decorated the method with [WebMethod] but still getting the error – Vivek Jul 23 '15 at 20:43
  • @Vivek exactly. You can't do that. It MUST be on an aspx or in a web service (asmx or WCF). – dmeglio Jul 23 '15 at 21:37

2 Answers2

1

I think you are a little bit confused, if you are trying to call a middle-tier method from the browser, there is something wrong. Think of the implications if this was supported. In my business logic layer I might have methods that are never exposed from the presentation layer. Perhaps a DropClientDatabase() used by some back end maintenance tool. What if I could directly call this method through JavaScript? That would mean anyone with a little bit of HTML and JavaScript skills could call DropClientDatabase() and cause a lot of problems for your users. Since this method is at the BLL layer, it's conceivable it doesn't do any kind of authentication checking directly (that might be a precondition of calling it), so poof, anyone can drop databases. We certainly would not want that.

We need to explicitly define the methods that can be called from AJAX partly as a security means. The List<User> ShowDetail() method in your Business layer should be called from your Presentation layer code (aspx.cs) like this

[WebMethod]
public static List<User> ShowDetail()
{
    //Call your method something like this
    var myVar = BLL.ShowDetail();
    //This will return your list in a JSON Format
    return myVar;
}

Once you did this, you can call it from JS using PageMethods or try changing the URL by this

url: "YourPageName.aspx/ShowDetail",
Community
  • 1
  • 1
Enrique Zavaleta
  • 2,098
  • 3
  • 21
  • 29
  • 1
    Curious, why are you invoking a serializer rather than just return myVar; and let the framework handle it? – dmeglio Jul 23 '15 at 21:38
  • @dman2306 Good catch, I thought the framework can't handle the convertion itself, because it is not a string, is a List variable what is returning the method, but reading the doc, it looks like it is internally called and handled. Good point – Enrique Zavaleta Jul 23 '15 at 22:35
0

This is how I have implemented in my project:

AJAX

$.ajax({
        url: 'DataViewer.aspx/GetData',
        dataType: "json",
        type: 'POST',
        data: {},
        contentType: "application/json; charset=utf-8",
        success: function (data) {           
        },
        error: function (d) {

        }
    });

ASP.NET

public partial class DataViewer: System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        [WebMethod]
        public static List<UserResult> GetData()
        {
            //No need to manually serialize the generic list
            return UserInfo.GetData();
        }

Note:

GetData() method in code behind should have [WebMethod] attribute and it should be public static.

JGV
  • 5,037
  • 9
  • 50
  • 94