使用 JQuery 调用 WebMethod

发布于 2024-07-14 08:44:03 字数 1876 浏览 4 评论 0原文

我无法通过 JQuery 调用进入我的 Search WebMethod。 也许有人可以帮助我指出正确的方向。

我还将所有内容打包到一个 zip 文件中,以防有人想仔细查看。

http://www.filedropper.com/jsonexample

谢谢 Ryan

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>JSON Example</title>
    <script type="text/javascript" language="JavaScript" src="jquery-1.3.1.min.js"></script>

<script type="text/javascript" language="javascript">

function Search() {
    var search = $("#searchbox").val();
    var options = {
        type: "POST",
        url: "Default.aspx/Search",
        data: "{text:" + search + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            alert('Success!');
        }
    };
    $.ajax(options);
}
</script>

</head>
<body>
    <form id="form1" runat="server">
        <input type="text" id="searchbox" size="40" />
        <a href="#" onclick="Search()" id="goSearch">Search</a>
        <br />        
        <div id="Load" />
    </form>
</body>
</html>

这是 default.aspx 背后的代码

 Imports System.Data
    Imports System.Web.Services
    Imports System.Web.Script.Serialization

    Partial Class _Default
        Inherits System.Web.UI.Page

        <WebMethod()> _
        Public Shared Function Search(ByVal text As String) As IEnumerable
            Return "test"
        End Function

    End Class

I am having trouble getting inside my Search WebMethod from my JQuery call.
Maybe someone could help to point me in the right direction.

I also packed up everything into a zip file incase someone wants to check it out for a closer look.

http://www.filedropper.com/jsonexample

Thanks
Ryan

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>JSON Example</title>
    <script type="text/javascript" language="JavaScript" src="jquery-1.3.1.min.js"></script>

<script type="text/javascript" language="javascript">

function Search() {
    var search = $("#searchbox").val();
    var options = {
        type: "POST",
        url: "Default.aspx/Search",
        data: "{text:" + search + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            alert('Success!');
        }
    };
    $.ajax(options);
}
</script>

</head>
<body>
    <form id="form1" runat="server">
        <input type="text" id="searchbox" size="40" />
        <a href="#" onclick="Search()" id="goSearch">Search</a>
        <br />        
        <div id="Load" />
    </form>
</body>
</html>

And here is the code behind for the default.aspx

 Imports System.Data
    Imports System.Web.Services
    Imports System.Web.Script.Serialization

    Partial Class _Default
        Inherits System.Web.UI.Page

        <WebMethod()> _
        Public Shared Function Search(ByVal text As String) As IEnumerable
            Return "test"
        End Function

    End Class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷爱 2024-07-21 08:44:03

要解决这样的问题,首先要做的是在 Firebug 中观察它。

如果您单击“搜索”链接并在 Firebug 控制台中观察 POST 请求/响应,您将看到它抛出 500 服务器错误:无效的 JSON 基元。

原因是“数据”JSON 文字中的键/值标识符没有被引用。 第 17 行应该是:

data: "{'text':'" + search + "'}",

然后,一切都会按预期工作。

注意:建议的数据 { test: search } 将不起作用。 如果您向 jQuery 提供实际的 JSON 文字而不是字符串,它会将其转换为 test=search 的键/值对,并发布它而不是 JSON。 这还会导致 ASP.NET AJAX ScriptService 或 PageMethod 抛出 Invalid JSON Primitive 错误。

To solve a problem like this, the first thing to do is watch it in Firebug.

If you click the "Search" link and watch the POST request/response in Firebug's console, you'll see it's throwing a 500 server error: Invalid JSON Primitive.

The reason for that is because the key/value identifiers in your "data" JSON literal aren't quoted. Line 17 should be:

data: "{'text':'" + search + "'}",

Then, all will work as expected.

Note: The suggested data { test: search } will not work. If you provide jQuery with an actual JSON literal instead of a string, it will convert that into a key/value pair of test=search and POST that instead of the JSON. That will also cause an ASP.NET AJAX ScriptService or PageMethod to throw the Invalid JSON Primitive error.

深海里的那抹蓝 2024-07-21 08:44:03

您需要执行以下操作 (C#):

  • WebMethod 必须是 public static
  • 它必须用 [WebMethod] 属性进行修饰
  • 上需要一个 ScriptManager 设置
  • 您的 .aspx页面 ScriptManager 的 EnablePageMethods="true"

这里是一些示例 javascript:

$().ready(function() {
    $(yourDropDownList).change(LoadValues);
});


function LoadValues() {
    PageMethods.YourMethod(arg1, CallSuccess, CallFailed);
}

function CallFailed(result) {
    alert('AJAX Error:' + result.get_message());
}

function CallSuccess(result) {
    //do whatever you need with the result
}

You need to do the following (C#):

  • The WebMethod must be public static
  • It must be decorated with the [WebMethod] attribute
  • You need a ScriptManager on your .aspx page
  • Set the ScriptManager's EnablePageMethods="true"

And here is some sample javascript:

$().ready(function() {
    $(yourDropDownList).change(LoadValues);
});


function LoadValues() {
    PageMethods.YourMethod(arg1, CallSuccess, CallFailed);
}

function CallFailed(result) {
    alert('AJAX Error:' + result.get_message());
}

function CallSuccess(result) {
    //do whatever you need with the result
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文