ajax调试需要帮助


<script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function () {
$('#myButton').click(function () {
$.ajax({
type: "POST",
url: "testajax.aspx/GetHello",
contentType: "application/json; charset=utf-8",
data: {},
dataType: "json",
success: function (data) {
$('#myLabel').text(data.d);
},
error: function (err) {
console.log(err);
}
});
});
});
</script>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<input id="myButton" type="button" value="Click me" />
<%--<asp:Button ID="myButton" runat="server" Text="Click me" />--%>
<asp:Label ID="myLabel" runat="server" Text="" />
</div>
</form>
</body>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public partial class DataUpdate__testa : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public string GetHello()
{
return "Hello";
}
}
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>

在web.config

中设置找了很久还是不知道怎么了。任何帮助将非常感激,谢谢!

                                      

你的后台代码看起来真的很乱。

从头开始。添加一个新的空白页,比如名为testjax .aspx。

那么,页面内部的代码将是这样的:

protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
static public string GetHello()
{
return "Hello";
}

不要乱动该页中的任何其他内容。事实上,当你输入上面的[WebMethod]时,vs会自动添加this:

using System.Web.Services;

但是,请仔细检查上面的"使用"存在于此测试页。

现在,当然我不会弄乱其余的代码或如何创建页面。(你不应该必须!!!!)。

因此,虽然我没有键入页面的其余部分,但完整的代码后面代码清单将是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CSharpWebApp
{
public partial class testajax : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
static public string GetHello()
{
return "Hello";
}
}

但是,当然你只在我们的简单例程GetHello的那个页面上输入了一个简单的web方法。

注意:

由于这不是一个单独的自定义"web服务页面,那么该页中的任何和所有方法必须被标记为静态。(向aspx页面添加方法是你的自由选择,或者像我经常做的那样,把它们塞到现有的网页中,但是对于现有的aspx页面,你必须像前面提到的那样将函数标记为静态。)

好的,现在是你的js代码。你有一大堆额外的尾随)::等等。编辑器应该会帮助你,但公平地说,js代码可能很难,因为它在构建过程中没有被编译或解析。

然而,我们的标记可以像这样:
<script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
function myjavatest() {
$.ajax({
type: "POST",
url: "testajax.aspx/GetHello",
contentType: "application/json; charset=utf-8",
data: {},
dataType: "json",
success: function (data) {
$('#myLabel').text(data.d);
},
error: function (err) {
console.log(err);
}
});
}
</script>

<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="myButton" runat="server" Text="Click me"
OnClientClick="myjavatest();return false" />
<br />
<asp:Label ID="myLabel" runat="server" Text=""  ClientIDMode="Static"/>

</div>
</form>

以上就是你所需要的。

一些事情:

如前所述,当你要添加web方法到一个现有的页面,然后确保它被标记为静态。

下:

要使用jQuery在页面上引用控件,然后使用clientid="Static"设置标签、文本框或其他内容。(因为asp.net处理器会混淆id名,这个设置可以防止这种情况)。

最新更新