首页 新闻 赞助 找找看

jquery调用ASP.net后台函数出错

0
悬赏园豆:30 [已关闭问题] 关闭于 2014-05-12 08:54
前台代码如下:
$.ajax({ type: "Post", async:true, url: "CodeOnlineEditor.aspx/SaveCode", data: "{'id':'"+id+"','content:'"+content+"'}", contentType: "application/json", dataType: "json", error: function (XHR) { alert("出错:" + XHR.responseText); }, success: function (data) { $("#div1").append(data.d + "
<br/>"); }, complete: function () { $("#div1").append("ajax访问后台方法完成<br/><br/>"); } });

后台代码:

[System.Web.Services.WebMethod()]
public string SaveCode(string id, string content)
{
……省略
}

弹出报错信息:

---------------------------
来自网页的消息
---------------------------
出错:<!DOCTYPE html>
<html>
<head>
<title>未知 Web 方法 SaveCode。<br>参数名: methodName</title>
<meta name="viewport" content="width=device-width" />
<style>
body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}
p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
pre {font-family:"Consolas","Lucida Console",Monospace;font-size:11pt;margin:0;padding:0.5em;line-height:14pt}
.marker {font-weight: bold; color: black;text-decoration: none;}
.version {color: gray;}
.error {margin-bottom: 10px;}
.expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
@media screen and (max-width: 639px) {
pre { width: 440px; overflow: auto; white-space: pre-wrap; word-wrap: break-word; }
}
@media screen and (max-width: 479px) {
pre { width: 280px; }
}
</style>
</head>

<body bgcolor="white">

<span><H1>“/”应用程序中的服务器错误。<hr width=100% size=1 color=silver></H1>

<h2> <i>未知 Web 方法 SaveCode。<br>参数名: methodName</i> </h2></span>

<font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">

<b> 说明: </b>执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

<br><br>

<b> 异常详细信息: </b>System.ArgumentException: 未知 Web 方法 SaveCode。<br>参数名: methodName<br><br>

<b>源错误:</b> <br><br>

<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code>

执行当前 Web 请求期间生成了未经处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。</code>

</td>…
---------------------------
确定
---------------------------

harrell的主页 harrell | 初学一级 | 园豆:14
提问于:2014-05-08 13:51
< >
分享
所有回答(3)
0

你把data: "{'id':'"+id+"','content:'"+content+"'}",改为data:null,SaveCode(string id, string content)的参数去掉,看一下能不能调用,可能是参数问题

单恋 | 园豆:678 (小虾三级) | 2014-05-08 14:13

去掉参数,改为null 问题依然存在

支持(0) 反对(0) harrell | 园豆:14 (初学一级) | 2014-05-08 14:37

@harrell: 进后台了吗?

支持(0) 反对(0) 单恋 | 园豆:678 (小虾三级) | 2014-05-08 14:40

@单恋: 就是没有进后台,所以才提示了“未知 Web 方法 SaveCode。”

支持(0) 反对(0) harrell | 园豆:14 (初学一级) | 2014-05-08 14:47

@harrell: 那可能是url的错误了。你百度找一下,或者url多改几个试试

支持(0) 反对(0) 单恋 | 园豆:678 (小虾三级) | 2014-05-08 16:12
0

一步步来 先把后台方法改为不带参数的,ajax方法也不用传参数,确定是否因为参数问题导致的

Zery | 园豆:6151 (大侠五级) | 2014-05-10 11:36
0
[System.Web.Services.WebMethod()]
public  static string SaveCode(string id, string content)
{
……省略
}

未加 static 关键字

随便取个名字算了 | 园豆:157 (初学一级) | 2017-12-16 23:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册