Skip to content Skip to sidebar Skip to footer

Call Method In Code-behind (secure Page) From Client (javascript)

Let me start of by saying I am very new to ASP.NET and C#. I have a simple web form containing data which I want to send to a code-behind page. The idea is to capture the data and

Solution 1:

Such static methods are called Page Methods in asp.net.

You call them form javascript like this on the same page as follows:

string data = "{mydata:myvalue}";
PageMethods.UpdateVendor(data);

You need to have a ScriptManager on the page and it should have PageMethods enabled

<asp:ScriptManagerrunat="server"EnablePageMethods="true"></asp:ScriptManager>

The page method must be defined on the page. It cannot be defined in a control, master page, or base page.

PageMethods only support HTTP POST(Even when you call them without the javascript proxy of PageMethods). You can read the details of this security limitation in this blog post by Scott Guthrie. Using the PUT verb is causing the 404 error.

ASP.NET AJAX 1.0 by default only allows the HTTP POST verb to be used when invoking web methods using JSON

Solution 2:

Your webmethod & javascript method should be on same page.

Solution 3:

Hi managed to sort out what was wrong.

I simply changed the HTTP method to "POST" as follows:

var saveOptions =
{
  url: "Profile.aspx/UpdateVendor",
  type: "POST",
  dataType: 'json',
  data: JSON.stringify({ vendor: ko.mapping.toJS(vendor) }),
  contentType: "application/json",

  success: function (response)
  {
  }
}

$.ajax(saveOptions);

This seemed to fix the problem and now I am able to send the JSON data to the codebehind method using AJAX.

Post a Comment for "Call Method In Code-behind (secure Page) From Client (javascript)"