0

I've been trying to call a VB.NET function from JavaScript for the website I've been working on, and found out about AJAX and started trying to use it.

(For reference, my target framework is 4.6.1.)

The following is a snippet of my VB.NET code, where PopulateDetailSection() is something which returns a string which is supposed to be the text from the div, but my breakpoint never hits this function.

System.Web.Services.WebMethod(EnableSession:=True, BufferResponse:=False)
    Protected Shared Function PopulateDetail() As HtmlGenericControl

        Return PopulateDetailSection()
    End Function

and as for the AJAX call:

jQuery.ajax({
                url: "frmActiveTrackingG.aspx/PopulateDetail",
                type: "GET",
                //contentType: "application/json: charset=utf-8",
                dataType:"html",
                success: function (data) {
                    alert(data);
                }
            });

I've tried alerting several things, but it keeps returning undefined unless I alert data which appears to return the aspx file starting with the header.

I usually don't ask questions here, but I'm truly stumped on this.

1
  • The code should be in an .asmx.vb file - create one by adding a new item to the project with a type of "Web Service". That will stop it from doing all the scaffolding, such as the header, that goes along with an .aspx item. Commented Sep 16, 2021 at 17:14

1 Answer 1

1

There some issues with your JavaScript. As described here: https://stackoverflow.com/a/5332290/428010 you need to post on the web page/method.

jQuery.ajax({
                url: "frmActiveTrackingG.aspx/PopulateDetail",
                type: "POST",
                contentType: "application/json: charset=utf-8",
                dataType: "json",
                success: function (data) {
                    alert(data);
                }
            });

On the VB side the method need to be public not protected.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.