来自客户端(JavaScript)的代码隐藏(安全页面)中的调用方法 [英] Call method in code-behind (secure page) from client (JavaScript)

查看:103
本文介绍了来自客户端(JavaScript)的代码隐藏(安全页面)中的调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先让我说我对ASP.NET和C#非常陌生。
我有一个简单的Web表单,其中包含要发送到代码隐藏页面的数据。
的想法是捕获数据并将其作为JSON对象发送到代码隐藏方法。
注意,这是通过JavaScript / AJAX完成的(请参见下面的代码)。
然后,代码隐藏方法将执行简单的HTTP PUT请求以更新数据。
.apsx页位于Secure文件夹中(使用Secure Master)。
不知道这是否会影响方法调用吗?

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 send it as a JSON object to a code-behind method. Note that this is done via JavaScript/AJAX (see code below). The code-behind method will then do a trivial HTTP "PUT" request to update the data. The .apsx page is located in the Secure folder (uses a Secure Master). Don't know if that will affect the method calling?

以下是我到目前为止的代码。

Following is the code I have thus far.

JavaScript / AJAX:

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

  success: function (response)
    {
    }
}

隐藏代码:

namespace PartyAtVendors.Secure
{
  [WebService]
  public partial class Profile : System.Web.UI.Page
  {
    [WebMethod]
    public static bool UpdateVendor(PartyAtApi.Models.Vendors vendor)
    {
      return true;
    }
  }
}

更新:

问题如下。未调用codebehind方法。当我运行并测试代码并使用Chrome的检查元素时,收到错误消息:

Problem is as follows. The codebehind method isn't called. When I run and test the code and use Chrome's "inspect element" I receive the error:

PUT http://localhost:50671/Secure/Profile.aspx/UpdateVendor 404 (Not Found)


推荐答案

您好设法找出问题所在。

Hi managed to sort out what was wrong.

我只是将HTTP方法更改为 POST,如下所示:

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);

这似乎可以解决问题,现在我可以使用以下方式将JSON数据发送至codebehind方法AJAX。

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

这篇关于来自客户端(JavaScript)的代码隐藏(安全页面)中的调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆