使用JQuery调用WebMethod [英] Using JQuery to call a WebMethod

查看:88
本文介绍了使用JQuery调用WebMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

谢谢 瑞安

    <%@ 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的背后代码

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

推荐答案

要解决此类问题,首先要做的就是在Firebug中观看它.

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

如果您单击搜索"链接并在Firebug的控制台中查看POST请求/响应,您将看到它引发500服务器错误:无效的JSON基元.

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.

其原因是因为未引用数据" JSON文字中的键/值标识符.第17行应为:

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.

注意::建议的数据{测试:搜索} 无效.如果为jQuery提供实际的JSON文字(而不是字符串),它将把它转换为键=值对test = search,然后将其转换为JSON而不是JSON.这也将导致ASP.NET AJAX ScriptService或PageMethod抛出无效的JSON基本错误.

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.

这篇关于使用JQuery调用WebMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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