简体   繁体   English

使用AJAX JQuery和ASP.Net与Master Pages提交表单

[英]Submit a form using AJAX JQuery and ASP.Net with Master Pages

I wrote a search program using lucene.net. 我用lucene.net写了一个搜索程序。 The search method returns a string containing an html table with the search results. 搜索方法返回包含带有搜索结果的html表的字符串。 This part works, but I wanted the ability to submit the search without reloading the entire page... So I searched and found that this could be done using AJAX. 这部分有效,但我希望能够在不重新加载整个页面的情况下提交搜索...所以我搜索并发现这可以使用AJAX完成。 For whatever reason I cannot get it to work. 无论出于何种原因,我无法让它发挥作用。

I'm not throwing an error. 我没有抛出错误。 The contents of "Search.aspx" get returned, but it seems like the Submit Method never executes. 返回“Search.aspx”的内容,但似乎提交方法永远不会执行。

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: 的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");
    }

}

You must have the ScriptModule configured in your web.config to call static page methods like that. 您必须在web.config中配置ScriptModule才能调用这样的静态页面方法。 If you are running an ASP.NET 3.5 project in Visual Studio using the built-in development web server, make sure this is in your web.config, inside system.web/httpModules : 如果使用内置开发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"/>

If you are using IIS then make sure this is in your web.config, inside system.webServer/httpModules : 如果您使用的是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"/>

If you don't have the right configuration, it is common that a post to Search.aspx/Submit will just return the full page and your web method will not even be called in that case. 如果您没有正确的配置,那么Search.aspx / Submit的帖子通常会返回完整页面,在这种情况下甚至不会调用您的Web方法。 The role of the ScriptModule is to map this request to the web method and return its return value as the response. ScriptModule的作用是将此请求映射到Web方法并将其返回值作为响应返回。


If that does't work (you already have the right configuration), then try to set your request contentType to application/json and perhaps also change the way you pass the query parameter to your web method (also in JSON format): 如果这不起作用(您已经有正确的配置),那么尝试将您的请求contentType设置为application/json ,也可能更改将查询参数传递给Web方法的方式(也是JSON格式):

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

See also these similar questions: 另请参阅以下类似问题:

Well I recent work with a site in ASP.NET, I add a Webservice to my ASP.NET project, and use this snippet in jQuery: 我最近在ASP.NET中使用了一个站点,我在我的ASP.NET项目中添加了一个Web服务,并在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!');
            }
        });

My Webservice are a function like this: 我的Webservice是这样的函数:

    [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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM