简体   繁体   English

Jquery AJAX与ASP.NET WebMethod返回整页

[英]Jquery AJAX with ASP.NET WebMethod Returning Entire Page

I'm doing some simple tests (in preparation for a larger project) to call an ASP.NET WebMethod using JQuery AJAX. 我正在做一些简单的测试(准备一个更大的项目)来使用JQuery AJAX调用ASP.NET WebMethod。 In my example, my WebMethod returns a simple string. 在我的示例中,我的WebMethod返回一个简单的字符串。 However, when I attempt to call it using JQuery, I get the entire HTML page content returned instead of just my string. 但是,当我尝试使用JQuery调用它时,我会返回整个HTML页面内容,而不仅仅是我的字符串。 What am I missing? 我错过了什么?

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

Check out this link. 看看这个链接。 I used some of his other posts to calll WCF service with success. 我使用了他的一些其他帖子来成功调用WCF服务。 Be sure to check out the related articles: 请务必查看相关文章:

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

I think I was getting confused with the "type" parameter in JQuery's $.post command. 我想我对JQuery的$ .post命令中的“type”参数感到困惑。 After talking to some folks, it seems that the return type for calling a WebMethod MUST be "json". 在与一些人交谈之后,似乎调用WebMethod的返回类型必须是“json”。 I was trying to use "html". 我试图使用“html”。 Once I changed it to "json" and then everything worked like normal. 一旦我将其改为“json”,然后一切正常。 So apparently, a method decorated with [WebMethod] returns JSON only, and that's where my hangup was. 显然,用[WebMethod]修饰的方法只返回JSON,这就是我的挂机所在。

Thanks for your replies guys. 谢谢你们的回复。

Try changing the last parameter "html" to "text". 尝试将最后一个参数“html”更改为“text”。 This parameter specifies the type of data to be returned. 此参数指定要返回的数据类型。

I had the exactly the same problem: WebMethod returned the entire HTML page instead the intended data. 我有完全相同的问题:WebMethod返回整个HTML页面而不是预期的数据。 For me, the solution came from changing inside ~/App_Start/RouteConfig.cs the following line: 对我来说,解决方案来自于更改〜/ App_Start / RouteConfig.cs内部的以下行:

settings.AutoRedirectMode = RedirectMode.Permanent;

to

settings.AutoRedirectMode = RedirectMode.Off;

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

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