jquery - 使用 AJAX JQuery 和 ASP.Net 与母版页提交表单

标签 jquery asp.net ajax forms

我使用 lucene.net 编写了一个搜索程序。 search 方法返回一个字符串,其中包含带有搜索结果的 html 表。这部分有效,但我希望能够在不重新加载整个页面的情况下提交搜索...所以我搜索并发现这可以使用 AJAX 来完成。无论出于何种原因,我都无法让它工作。

我没有抛出错误。 “Search.aspx”的内容被返回,但提交方法似乎从未执行。

搜索.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Search.aspx.cs" Inherits="Search" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">

<script type="text/javascript">
    $(function () {    

        $(".sBM").click(function () {

            dataString = "valve"

            $.ajax({
                type: "POST",
                url: "Search.aspx/Submit",
                //data: dataString,
                data: dataString,
                contentType: "application/html; charset=utf-8",
                dataType: "html",
                success: function (msg) {
                    $("#searchResults").text(msg);
                   alert(msg);
                },
                error: function (xhr, ErrorText, thrownError) {
                    $("#searchResults").text("Error" + xhr.status);
                }

            });
            return false;
        });

    }); 

</script>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">

 <div class="sHead"> 

    <div id="search_form" class="sSBM">
    <form name="search" action=""> 
        <fieldset>  
            <label for="name" id="rpe_label">RPE Search</label>  
            <input type="text" name="query" value="" class="sTM" />
            <input type="submit" name="submit" class="sBM" id="submit_btn" value="" />               
      </fieldset>  
    </form>  
    </div>  

 </div>   

   <div id="searchResults" ></div>     
</asp:Content>

代码文件:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

public partial class Search : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    [WebMethod]

    public static string Submit(string query) 
    {
        SearchDoc seek = new SearchDoc();
        return seek.Search("valve");
    }

}

最佳答案

您必须在 web.config 中配置 ScriptModule 才能调用此类静态页面方法。如果您使用内置开发 Web 服务器在 Visual Studio 中运行 ASP.NET 3.5 项目,请确保它位于 web.config 中的 system.web/httpModules 内:

<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

如果您使用的是 IIS,请确保它位于您的 web.config 中的 system.webServer/httpModules 内:

<remove name="ScriptModule"/>
<add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

如果您没有正确的配置,通常情况下,发送到 Search.aspx/Submit 的帖子将仅返回完整页面,并且在这种情况下甚至不会调用您的 Web 方法。 ScriptModule 的作用是将此请求映射到 Web 方法,并将其返回值作为响应返回。

<小时/>

如果这不起作用(您已经拥有正确的配置),请尝试将请求 contentType 设置为 application/json,也许还可以更改将查询参数传递到 Web 的方式方法(也是JSON格式):

data: { "query": dataString }, 
contentType: "application/json; charset=utf-8",
dataType: "json",

另请参阅这些类似的问题:

关于jquery - 使用 AJAX JQuery 和 ASP.Net 与母版页提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8610625/

相关文章:

jquery - 将 select2 应用于克隆节点中的元素?

jquery - 查找元素的索引

C# ASP.net 渲染 HTML 字符串

asp.net - Shibboleth 和 .NET

php - 复选框数据在单击时动态保存到数据库

javascript - 如何在客户端检查 YouTube 上是否存在视频

javascript - ASYNC AJAX CALLBACK 遇到问题

javascript - jQuery AJAX 不重定向?

jquery - 使用 JQuery 自动刷新内容而不重新加载页面?

c# - 在 C# 中使用 json 对象