调用在asp.net web表单jQuery的一个WebMethod [英] Calling a webmethod with jquery in asp.net webforms

查看:123
本文介绍了调用在asp.net web表单jQuery的一个WebMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的WebMethod,它是不打断点上的webmethod设置

CS:

  [的WebMethod]
公共静态字符串搜索()
{
    返回工作;
}

ASPX:

 搜索功能(){
    $阿贾克斯({
        键入:POST,
        网址:ProcessAudit / req_brws.aspx /搜索
        数据:{},
        的contentType:应用/ JSON的;字符集= UTF-8,
        数据类型:JSON
        成功:函数(MSG){
            警报(MSG)
        }
    });
}
  <按钮ID =btnSearch的onclick =搜索()>搜索和LT; /按钮>


解决方案

请确保您已在的ScriptManager 元素启用页面方法:

 < ASP:的ScriptManager ID =SCM=服务器的EnablePageMethods =真/>

和您通过返回false的onclick处理程序中,取消按钮的默认操作,否则页面执行完全回发,你的AJAX调用可能永远不会有结束的时候。这里有一个完整的工作的例子:

 <%@页面语言=C#%>
<脚本类型=文/ C#=服务器>
[System.Web.Services.WebMethod]
公共静态字符串搜索()
{
    返回工作;
}
< / SCRIPT><!DOCTYPE HTML>
< HTML和GT;
<头ID =头像1=服务器>
    <标题>< /标题>
< /头>
<身体GT;
    <表ID =Form1的=服务器>
        < ASP:的ScriptManager ID =SCM=服务器的EnablePageMethods =真/>
        <按钮ID =btnSearch的onclick =搜索();返回false; >搜索和LT; /按钮>
    < /表及GT;    <脚本类型=文/ JavaScript的SRC =htt​​p://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js>< / SCRIPT>
    <脚本类型=文/ JavaScript的>
        功能搜索(){
            $阿贾克斯({
                输入:POST,
                网址:'<%= RESOLVEURL(〜/ Default.aspx的/搜索)%>,
                数据:{},
                的contentType:应用/ JSON的;字符集= UTF-8,
                数据类型:JSON,
                成功:函数(MSG){
                    警报(msg.d)
                }
            });
        }
    < / SCRIPT>
< /身体GT;
< / HTML>

另一种可能性是订阅点击处理程序悄悄地:

 <按钮ID =btnSearch>搜索和LT; /按钮>

,然后里面单独的JavaScript文件:

  $('#btnSearch')。点击(函数(){
    $阿贾克斯({
        输入:POST,
        网址:'<%= RESOLVEURL(〜/ Default.aspx的/搜索)%>,
        数据:{},
        的contentType:应用/ JSON的;字符集= UTF-8,
        数据类型:JSON,
        成功:函数(MSG){
            警报(msg.d)
        }
    });
    返回false;
});

您也可能会注意到的成功回调中的 msg.d 财产ASP.NET使用来包装整个响应进入以及的用法的用法 RESOLVEURL 方法正确生成的URL页面方法,而不是硬编码的。

I have the following webmethod and it isn't hitting the breakpoint set on the webmethod

cs:

[WebMethod]
public static string search()
{
    return "worked";
}

aspx:

  function search() {
    $.ajax({
        type: "POST",
        url: "ProcessAudit/req_brws.aspx/search",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            alert(msg)
        }
    });
}
  <button id = "btnSearch" onclick = "search()" >Search</button>

解决方案

Make sure that you have enabled page methods in your ScriptManager element:

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

and that you have canceled the default action of the button by returning false inside the onclick handler, otherwise the page performs a full postback and your AJAX call might never have the time to finish. Here's a full working example:

<%@ Page Language="C#" %>
<script type="text/c#" runat="server">
[System.Web.Services.WebMethod]
public static string search()
{
    return "worked";
}
</script>

<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="Form1" runat="server">
        <asp:ScriptManager ID="scm" runat="server" EnablePageMethods="true" />
        <button id="btnSearch" onclick="search(); return false;" >Search</button>
    </form>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script type="text/javascript">
        function search() {
            $.ajax({
                type: 'POST',
                url: '<%= ResolveUrl("~/default.aspx/search") %>',
                data: '{ }',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (msg) {
                    alert(msg.d)
                }
            });
        }
    </script>
</body>
</html>

Another possibility is to subscribe to the click handler unobtrusively:

<button id="btnSearch">Search</button>

and then inside a separate javascript file:

$('#btnSearch').click(function() {
    $.ajax({
        type: 'POST',
        url: '<%= ResolveUrl("~/default.aspx/search") %>',
        data: '{ }',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (msg) {
            alert(msg.d)
        }
    });
    return false;
});

You might also notice the usage of the msg.d property inside the success callback which ASP.NET uses to wrap the entire response into as well as the usage of the ResolveUrl method to properly generate the url to the page method instead of hardcoding it.

这篇关于调用在asp.net web表单jQuery的一个WebMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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