在调用方法code-背后(安全网页)的客户端(javascipt的) [英] Call method in code-behind (secure page) from client (JavaScipt)

查看:162
本文介绍了在调用方法code-背后(安全网页)的客户端(javascipt的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我先说我是很新的ASP.NET和C#开始的。 我有一个包含我要发送至code隐藏页数据的简单Web表单。 这样做是为了捕获数据,并将其作为JSON对象到code隐藏方法来发送。 请注意,这是通过JavaScript / AJAX(见下文code)完成。 在code隐藏方法,然后做一个简单的HTTPPUT的请求来更新数据。 该.apsx页面位于安全的文件夹(使用安全大师)。 不知道这是否会影响到方法调用?

以下是code我迄今。

的JavaScript / AJAX:

  VAR saveOptions =
{
  网址:Profile.aspx / UpdateVendor
  键入:PUT
  数据类型:JSON,
  数据:JSON.stringify({供应商:ko.mapping.toJS(供应商)}),
  的contentType:应用/ JSON

  成功:函数(响应)
    {
    }
}
 

code-背后:

 命名空间PartyAtVendors.Secure
{
  [WebService的]
  公共部分类简介:System.Web.UI.Page
  {
    [WebMethod的]
    公共静态布尔UpdateVendor(PartyAtApi.Models.Vendors供应商)
    {
      返回true;
    }
  }
}
 

更新:

问题如下。在codebehind方法不叫。当我运行和测试code和使用Chrome的检查元素我收到错误:

  PUT HTTP://本地主机:50671 /安全/ Profile.aspx / UpdateVendor 404(未找到)
 

解决方案

喜设法弄清楚什么是错误的。

我只是改变了HTTP方法为POST,如下所示:

  VAR saveOptions =
{
  网址:Profile.aspx / UpdateVendor
  键入:POST,
  数据类型:JSON,
  数据:JSON.stringify({供应商:ko.mapping.toJS(供应商)}),
  的contentType:应用/ JSON

  成功:函数(响应)
  {
  }
}

$阿贾克斯(saveOptions);
 

这似乎是解决这个问题,现在我能够将JSON数据发送到使用AJAX的codebehind方法。

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)
    {
    }
}

Code-behind:

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

Update:

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.

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.

这篇关于在调用方法code-背后(安全网页)的客户端(javascipt的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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