2011-12-22 50 views
1

我使用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"); 
    } 

} 

回答

4

你必須在你的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 /提交將只返回整個頁面,在這種情況下甚至不會調用Web方法。 ScriptModule的作用是將此請求映射到Web方法並返回其返回值作爲響應。


如果開不工作(你已經正確的配置),然後嘗試您的請求的contentType設置爲application/json,或許也會改變你傳遞查詢參數的方式到你的Web方法(也JSON格式):

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

又見這些類似的問題:

0

在ASP.NET網站嗯,我最近的工作,我一個WebService添加到我的ASP.NET項目,並使用此代碼段jQuery中:

 var info = {}; 
     info.Ticket = ticket; 
     info.idCategoria = $('#hidCategoria').val(); 

     var DTO = { 'info': info }; 

     $.ajax({ 
      type: "POST", 
      contentType: "application/json; charset=utf-8", 
      url: "MyWebService.asmx/MyTargetFunction", 
      data: JSON.stringify(DTO), 
      dataType: "json", 
      success: function (data) { 
       if (data.d != null) { 
        // My Logic 
       } 
      }, 
      error: function (data) { 
       alert('Error!'); 
      } 
     }); 

我的web服務就像一個功能這個:

[WebMethod] 
public ResponseInfo CrearTicket(CreateTicketInfo info) { 
    ResponseInfo i = new ResponseInfo(); 
    _info = info; 

    try 
    { 
     // Logic Here 
    } 
    catch (Exception e) 
    { 
     i.ResponseCode = ContactoConstants.GENERICERROR; 
     i.Message = e.Message; 
    } 

    return i; 
}