Using Web Methods in Acumatica and Calling Them with AJAX from an ASPX Page
One of the ways to interact with server-side code is through web methods, which can be invoked asynchronously using AJAX (Asynchronous JavaScript and XML) from client-side scripts. This article will guide you through creating web methods in Acumatica and calling them using AJAX from an ASPX page.
Setting Up the ASPX Page with Master Pages
At the top of your ASPX page, include the following directives to specify the master page and its type:
<%@ Page Language="C#" MasterPageFile="~/MasterPages/FormView.master" AutoEventWireup="true" ValidateRequest="false" CodeFile="ACPR3000.aspx.cs" Inherits="Page_ACPR3000" Title="Untitled Page" %>
<%@ MasterType VirtualPath="~/MasterPages/FormView.master" %>
With this master page, you can add the following section where we can include custom styles and scripts:
Additionally, EnablePageMethods should be enabled:
Creating a Web Method in Acumatica
In Acumatica, web methods are public static methods defined in the code-behind of an ASPX page or a user control. They are decorated with the [WebMethod] attribute, which makes them accessible for AJAX calls.
Example: Defining a Web Method
Below is an example of a web method that retrieves tasks based on a project code:
Key Points:
-
The method is public and static.
-
It is decorated with the [WebMethod] attribute.
-
Parameters can be passed to the method (e.g., string projectCD).
-
The method returns data that can be serialized to JSON.
Calling the Web Method Using AJAX
To invoke the web method from the client side, you can use AJAX. jQuery simplifies AJAX calls and is widely used in web applications.
Including jQuery
Ensure that jQuery is included in your page, as shown before. You can use the CDN link or host it locally.
Writing the AJAX Function
To take your Acumatica development skills to the next level and ensure your solution fits your unique business needs, we’re here to help. If you’re looking for custom web methods or any tailored functionality within Acumatica, don’t hesitate to reach out. Our team specializes in creating seamless integrations and customizations to enhance your business processes.
Ready for a custom solution? Contact us today with your customization request and let’s bring your vision to life!