3

I can't find a way to pass values from a javascript file, (that I got from a html file from an onclick event), to a aspx.cs file.

I tried using the following code. Every article seems to show this way but the error keeps getting called, also tried with 'type' instead of 'method'.

.js file:

//get 'name' variable here
        $.ajax({
                url: 'WebForm.aspx/Send',
                dataType: "json",
                method: 'post',
                contentType: "application/json; charset=utf-8",
                data: { name: name },
                success: msgsuccess,
                error: msgerror
            });

WebForm.aspx.cs file:

    [WebMethod]
    public static string Send(string name)
    {//breakpoint here
        //code
    }

The code never gets to the breakpoint in the aspx.cs file, therefore the code value never gets passed.

This is my first post in stack overflow, sorry if anything is wrong formatted.

  • Press F12 in your browser - any errors in console? If you run the AJAX with the *Network* tab open, what do you see? – Tyler Roper Feb 05 '19 at 18:19
  • @TylerRoper I do get this error when I press the button with the event: **http://localhost:50301/WebForm.aspx/Send 401 (Unauthorized)**. In the network tab there's a green bar raising to 60 ms but can't see how that is relevant. – Diogo Almeida Feb 06 '19 at 10:32

3 Answers3

2

You need to add a ScriptManager control to the page (or its MasterPage) and make sure that the EnablePageMethods property of the ScriptManager is true.

For example, when the page or its master page have a ScriptManager control like this:

<asp:ScriptManager runat="server">
        ...
</asp:ScriptManager>

then add the EnablePageMethods property and set it to true:

<asp:ScriptManager runat="server" EnablePageMethods="true">
        ...
</asp:ScriptManager>

Note that you cannot add a ScriptManager control to a page when its master page already has one.


Also, you can simplify calling the method from JavaScript. You can simply use:

PageMethods.Send(name, msgsuccess, msgerror);
NineBerry
  • 26,306
  • 3
  • 62
  • 93
  • Added the ScriptManager control, doesn't seem to work. Also I didn't know where exactly where to put it so I researched and was adviced to put it right after the form control, so I inserted it like this: `
    ...`
    – Diogo Almeida Feb 06 '19 at 10:38
1

[WebMethod]s parse JSON, so you need to have your request data sent as a string:

$.ajax({
  url: 'WebForm.aspx/Send',
  dataType: "json",
  method: 'post',
  contentType: "application/json; charset=utf-8",
  data: JSON.stringify({ name: name }), // use JSON.stringify here
  success: msgsuccess,
  error: msgerror
});

Everything else should work as expected.

R Y
  • 455
  • 1
  • 3
  • 13
1

Problem Solved:

Checked the console (with F12) as adviced from @TylerRope and got error number 401 (Unauthorized).

With that I resourced to this post's correct answer and solved the error. If you know any side effects that this solution might cause would appreciate if you share.

Thank you for your help!