jquery+ajax 如何调用页面后台方法?
查了很多资料,做了demo,没有成功,下面是我的代码:前台js,引用了jquery库:
<script type="text/javascript">
$(function(){
$("#btn1").click(function(){
$.ajax({
//要用post方式
type: "Post",
//方法所在页面和方法名
url: "Demo.aspx/SayHello",
data:null,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
//返回的数据用data.d获取内容
alert(data.d);
},
error: function(err) {
alert("errorinfo:"+err);
}
});
//禁用按钮的提交
return false;
})
})
</script>
[b]Demo.aspx.cs中的方法:[/b]
[System.Web.Services.WebMethod()]
public static string SayHello()
{
return "Hello Ajax!";
}
请求一下大家,jquery + ajax 如何调用后台的方法?
--------------------编程问答-------------------- url: "Demo.aspx/SayHello",
这里是直接调用 页面,不用写方法名
根据访问的页面返回参数
你直接改成url: "Demo.aspx",
public void ProcessRequest(HttpContext context)
{
context.Response.Write(SayHello());
} --------------------编程问答-------------------- 还有一个你返回的对象也不是json格式,直接把ajax中的dataType: "json", 改成dataType: "html", --------------------编程问答-------------------- $.ajax({
type:"POST",
url:"Demo.aspx",
dataType:"html",
cache:false,
data:'num='+Math.round(Math.random()*10000),
error:function(XmlHttpRequest,textStatus, errorThrown)
{
alert(XmlHttpRequest.responseText);
},
success:function(data1){
alert(data1);
}
}); --------------------编程问答-------------------- protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Hello Ajax!");
} --------------------编程问答-------------------- 1.楼上几个回答你可以忽略
2.查msdn看WebMethod,ScriptMethod两个特性的用法
3.在现代点的浏览器里按F12,启动调试器。查看xhr数据请求封包里面的内容。
ps:玩ajax的不会用浏览器的F12基本等于没入门 --------------------编程问答-------------------- +10086 --------------------编程问答-------------------- +10010
返回json,你得符合json格式,你这是输出的字符串,怎么会有data.d呢。json用序列化javascriptserializer。
JavaScriptSerializerser = new JavaScriptSerializer();ser.Serialize(要序列化的object); --------------------编程问答-------------------- 楼上说的对。。要学会调试啊。。。浏览器调试。。 --------------------编程问答--------------------
没必要用$.ajax,这里url是只写Demo.aspx没错,这不是webservice
$.post("Demo.aspx", function (data) {
alert(data);
});
完了在cs里写protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Hello Ajax!");
Response.End();
}
如果调试还是没进pageload的话,说明url有问题,你可以直接在浏览器中查看Demo.aspx页面,如果报错,你看看是不是Demo页面上<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="test.Demo" %>Inherits中没写命名空间,只有个.Demo,没有果断加上。 --------------------编程问答-------------------- 楼上所有人都在这里胡咧咧,别听他们虾扯蛋。
给你个能跑的例子。
$.ajax({
type: "post",
url: "Detail.aspx/GetSignature",
data: "{'str':'" + st + "'}",
contentType: "application/json;charset=utf-8;",
dataType: "json",
success: function (data) {
alert(data.d);
},
error: function (e) {
}
})
[WebMethod]--------------------编程问答--------------------
public static string GetSignature(string str) {
return "test";
}
--------------------编程问答-------------------- 目测楼主要晕了 --------------------编程问答--------------------
$('#send_ajax').click(function () {
$.ajax({
type: "POST",
contentType: "application/json;charset=utf-8",
url: "WebServicetest.asmx/HelloWorld",
data: "{username:\"" + document.getElementById('input1').value + "\"}",
//"{username:'" + document.getElementById('input1').value + "'}"
dataType: "json",
success: function (data) {
// alert(data);
// alert(data.d);
var json = null;
json = eval('(' + data.d + ')')
alert(json.username);
},
beforeSend: function (x) {
x.setRequestHeader("Content-Type", "application/json; charset=utf-8");
},
error: function (x, e) {
alert(x.responseText);
},
complete: function (x) {
alert(x.responseText);
}
});
});
+1 --------------------编程问答--------------------
这个也是对的 --------------------编程问答-------------------- 正确答案就在这里,呵呵慢慢找 --------------------编程问答-------------------- 10L正解 7L扯淡 会有.d的 自己去试试就知道了 --------------------编程问答-------------------- 情何以堪
补充:.NET技术 , ASP.NET