使用 ASP.NET WebMethod 返回整个页面的 Jquery AJAX

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/348689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:00:42  来源:igfitidea点击:

Jquery AJAX with ASP.NET WebMethod Returning Entire Page

asp.netjqueryajax

提问by Dana

I'm doing some simple tests (in preparation for a larger project) to call an ASP.NET WebMethod using JQuery AJAX. In my example, my WebMethod returns a simple string. However, when I attempt to call it using JQuery, I get the entire HTML page content returned instead of just my string. What am I missing?

我正在做一些简单的测试(为更大的项目做准备)以使用 JQuery AJAX 调用 ASP.NET WebMethod。在我的示例中,我的 WebMethod 返回一个简单的字符串。但是,当我尝试使用 JQuery 调用它时,我返回了整个 HTML 页面内容,而不仅仅是我的字符串。我错过了什么?

Client Side :

客户端 :

$(document).ready(function ready() {
        $("#MyButton").click(function clicked(e) {
            $.post("Default.aspx/TestMethod",
                {name:"Bob"},
                function(msg) {
                    alert("Data Recieved: " + msg);
                },
                "html"
            );
        });
    });

Server Side:

服务器端:

using System;
using System.Web.Services;

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

        }

        [WebMethod]
        public static string TestMethod(string name)
        {
            return "The value submitted was " + name;
        }
    }
}

采纳答案by JoshBerke

Check out this link. I used some of his other posts to calll WCF service with success. Be sure to check out the related articles:

看看这个链接。我使用他的一些其他帖子成功调用了 WCF 服务。请务必查看相关文章:

http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/

http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/

Read through the article but its essentially:

通读这篇文章,但它的本质是:

  $("#Result").click(function() {
    $.ajax({
      type: "POST",
      url: "Default.aspx/GetDate",
      data: "{}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(msg) {
        $("#Result").text(msg.d);
      }
    });
});

回答by Dana

I think I was getting confused with the "type" parameter in JQuery's $.post command. After talking to some folks, it seems that the return type for calling a WebMethod MUST be "json". I was trying to use "html". Once I changed it to "json" and then everything worked like normal. So apparently, a method decorated with [WebMethod] returns JSON only, and that's where my hangup was.

我想我对 JQuery 的 $.post 命令中的“type”参数感到困惑。在与一些人交谈后,似乎调用 WebMethod 的返回类型必须是“json”。我试图使用“html”。一旦我将其更改为“json”,然后一切正常。很明显,用 [WebMethod] 修饰的方法只返回 JSON,这就是我挂断的地方。

Thanks for your replies guys.

谢谢各位的回复。

回答by rajesh pillai

Try changing the last parameter "html" to "text". This parameter specifies the type of data to be returned.

尝试将最后一个参数“html”更改为“text”。此参数指定要返回的数据类型。

回答by BernieSF

I had the exactly the same problem: WebMethod returned the entire HTML page instead the intended data. For me, the solution came from changing inside ~/App_Start/RouteConfig.csthe following line:

我遇到了完全相同的问题:WebMethod 返回了整个 HTML 页面而不是预期的数据。对我来说,解决方案来自在~/App_Start/RouteConfig.cs内部更改 以下行:

settings.AutoRedirectMode = RedirectMode.Permanent;

to

settings.AutoRedirectMode = RedirectMode.Off;