我使用lucene.net編寫了一個搜索程序。 search方法返回一個包含html表格和搜索結果的字符串。這部分工作,但我希望能夠提交搜索,而無需重新加載整個頁面...所以我搜索,發現這可以使用AJAX來完成。無論出於何種原因,我無法實現它的工作。使用AJAX JQuery和ASP.Net與主頁提交表單
我不會拋出一個錯誤。 「Search.aspx」的內容得到返回,但看起來像提交方法從不執行。
Search.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>
的CodeFile:
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");
}
}