【问题标题】:Jquery , Ajax and ASP.NET webmethodsJquery、Ajax 和 ASP.NET 网络方法
【发布时间】:2009-08-28 17:20:52
【问题描述】:

如果 HTTP 请求是 POST,则使用查询 ajax 在 ASP.NET 页面中调用 webmethod 效果很好。如果使用 HTTP GET 请求,则运行 Page_Load 事件而不是 web 方法。 有什么想法吗?

这里是阿贾克斯

$.ajax({
        type: "GET",
        url: "http://local.proleaguesports.pagefad.com/AjaxTesting.aspx/receivermethod",
        data: "{'test':'MyName'}",
        contentType: "application/json",
        dataType: "json",
        success: mycallback,
        error: handler
    });

这是 C# 中的代码

public partial class AjaxTesting : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("Page_Load runs instead of the receivermethod below");
    }

    [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public static string receivermethod()
    {

        return "test received";

    }
}

【问题讨论】:

  • 你为什么使用Get?如果您在 Internet 上进行一些阅读,您会发现它会带来一些重大的安全漏洞。 “发布”不起作用吗?
  • John,你能推荐一篇好的安全和 HTTPGET 文章吗?我想我会使用 POST。但是,现在我很好奇:)
  • 您的 ajax 调用正在传回数据,但您的 WebMethod 没有参数

标签: asp.net jquery ajax


【解决方案1】:

您可以考虑将您的 webmethods 移动到一个单独的 web service/asmx 中。我在某处听说在 aspx 中调用 webmethod 会导致整个页面重新加载,但目前我找不到确认参考。

【讨论】:

  • 不,它没有。我有一些网络方法可以正常工作并且不会重新加载页面。
  • 我刚刚在具有多个 webmethod 的页面的 page_load 方法中设置了一个断点。调用 web 方法确实会触发 Page_Load(),我假设所有其他暗示。
猜你喜欢
  • 2014-02-17
  • 1970-01-01
  • 2012-04-16
  • 1970-01-01
  • 2019-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-16
相关资源
最近更新 更多