Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么这个AJAX调用返回整个页面内容?_C#_Jquery_Asp.net_Ajax_Webmethod - Fatal编程技术网

C# 为什么这个AJAX调用返回整个页面内容?

C# 为什么这个AJAX调用返回整个页面内容?,c#,jquery,asp.net,ajax,webmethod,C#,Jquery,Asp.net,Ajax,Webmethod,我正在尝试调用在我的一个aspx页面文件中找到的以下webmethod: [WebMethod] public static string GetReportDetails() { var reportDetails = DataAccess.Database().GetBusinessInterestReportDetails(HttpContext.Current.User.Identity.Name); var json = BusinessInterestReport.G

我正在尝试调用在我的一个aspx页面文件中找到的以下webmethod:

[WebMethod]
public static string GetReportDetails()
{
    var reportDetails = DataAccess.Database().GetBusinessInterestReportDetails(HttpContext.Current.User.Identity.Name);
    var json = BusinessInterestReport.GetJson(reportDetails); 
    return json;
}
这是我用来调用webmethod的javascript:

 $.ajax({
      type: 'POST',
      url: 'SummaryReport.aspx/GetReportDetails',
      dataType: 'json',
      success: function (data) {
           alert(data);
      },
      error: function (jqXHR, textStatus, errorThrown) {
           alert('An error has occured: ' + errorThrown);
      }
 });
进行此ajax调用的javascript:

$('.reportOption').click(function (e) {
    $.ajax({
      type: 'POST',
      url: 'SummaryReport.aspx/GetReportDetails',
      dataType: 'json',
      success: function (data) {
           alert(data);
      },
      error: function (jqXHR, textStatus, errorThrown) {
           alert('An error has occured: ' + errorThrown);
      }
 });
})
ScriptModule
config已在
web.config
中。在webmethod上甚至没有找到断点,整个页面的内容都会返回。知道是什么引起的吗

编辑: 使用Chrome的调试控制台,我发现了以下错误:

[ArgumentException: Unknown web method GetReportDetails.
Parameter name: methodName]
   System.Web.Script.Services.WebServiceData.GetMethodData(String methodName) +516665
   System.Web.Handlers.ScriptModule.OnPostAcquireRequestState(Object sender, EventArgs eventArgs) +168
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +68
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
为什么它不选择方法名?我还使用


p.S.刚刚意识到我是从一个iFrame中调用它的。这可能与问题有关吗?

我认为您需要显式添加
contentType
,因为它的默认值是
application/x-www-form-urlencoded;charset=UTF-8
,这不是您想要的

因此,您可能需要稍微修改一下jQuery代码

$.ajax({
  type: "POST",
  contentType: "application/json; charset=utf-8",
  url: "SummaryReport.aspx/GetReportDetails",
  dataType: "json",
  success: function (data) {
      alert(data);
  },
  error: function (jqXHR, textStatus, errorThrown) {
      alert('An error has occured: ' + errorThrown);
  }
});

GetReportDetails
方法中删除
static
关键字。

不要在web方法中编写管道代码(JSON序列化/反序列化)。只需获取/返回模型(.NET POCO对象):

然后消费:

$.ajax({
    type: 'POST',
    url: 'SummaryReport.aspx/GetReportDetails',
    contentType: 'application/json',
    success: function (data) {
        // the actual object will be inside data.d
        var reportDetails = data.d;
        // Now you could directly use the properties of your model
        // For example if your ReportDetails .NET type had a string property
        // called FooBar you could directly alert(reportDetails.FooBar);
    },
    error: function (jqXHR, textStatus, errorThrown) {
        alert('An error has occured: ' + errorThrown);
    }
});
注意事项:

  • 我们已经明确指定了
    contentType:'application/json'
    ,以向web方法表明我们希望使用json作为传输机制
  • 我们已经去掉了
    dataType:'json'
    属性,因为jQuery足够智能,可以使用Content-Type响应头并自动解析传递给
    success
    回调的
    data
    参数

我还建议您阅读这篇文章。

修复了它。结果表明,aspx文件顶部缺少inherits属性

所以现在我有:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SummaryReport.aspx.cs" MasterPageFile="~/MasterPages/SummaryReports.Master"
Inherits="Web.SummaryReport"
    %>

根据我在这个问题上的经验,我想在此补充一点。 如果您使用的是内容页或母版页,您不能通过访问该页来调用WebMethod,而是添加一个webserivce页并使用它,请参阅与我一起使用的此链接。

我在这儿抄的

继承System.Web.Services.WebService

<WebMethod()> _
Public Function MyFunction() As String
    Return "Hello World"
End Function
_
作为字符串的公共函数MyFunction()
返回“你好,世界”
端函数
然后,您可以从母版页或内容页调用webmethod,如下所示:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    function MyFunction() {
        var request = $.ajax({
            type: 'POST',
            url: 'HelloWord.asmx/MyFunction',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (serverdata) {
                alert("Done  " + serverdata);
            },
            error: function (error) {
                alert("error  ");
            }
        });
        return false;
    }
</script>

函数MyFunction(){
var请求=$.ajax({
键入:“POST”,
url:'HelloWord.asmx/MyFunction',
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:函数(serverdata){
警报(“完成”+服务器数据);
},
错误:函数(错误){
警报(“错误”);
}
});
返回false;
}
请确保取消对该行的注释: System.Web.Script.Services.ScriptService
在webservice页面中,因为在创建页面时,如果未点击webmethod,它将被注释

,它是否返回
500
404
错误页面的代码?请显示包含按钮的页面的HTML(或其他内容)变量json包含什么?我见过的大多数示例包括
contentType:'application/json;字符集=utf-8'
。这可能与此有关吗?您说您正在“aspx”文件中调用它-您的ScriptManager中是否启用了PageMethods?谢谢!这对我有帮助。我应该抓住那个。。。我没有明确地设置contentType。
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    function MyFunction() {
        var request = $.ajax({
            type: 'POST',
            url: 'HelloWord.asmx/MyFunction',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (serverdata) {
                alert("Done  " + serverdata);
            },
            error: function (error) {
                alert("error  ");
            }
        });
        return false;
    }
</script>