javascript ajax访问.net静态方法??

hackerxxw 2010-04-26 09:33:39
jquery的 ajax方法访问 .net静态方法
1、c# 静态方法

[System.Web.Services.WebMethod]
public static string GetMsg(string id)
{
return "user is " + id;
}

2、jquery方法 可以正确返回

$.ajax({
type: "POST",
url: "Default.aspx/GetMsg",
data: "{'id':'admin'}",
contentType: "application/json; charset=utf-8",
success: function(msg) { alert(msg); }
})

3、javascript 自己写的xmlhttprequest访问不了静态方法
希望了解这方面的大侠 指点一下 啊

var ajax = function(){};

var isUndefined = function(variable) {
return typeof variable == 'undefined' ? true : false;
}

var httpSuccess = function(req) {
try {
return !req.status && location.protocol == "file:" ||
(req.status >= 200 && req.status < 300) || req.status == 304 ||
browser.safari && isUndefined(req.status);
} catch (e) { }
return false;
};

ajax.CreateXMLHttp = function() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if (window.ActiveXObject) {
try { return new ActiveXObject("Microsoft.XMLHTTP"); }
catch (e) {
try { return new ActiveXObject("Msxml2.XMLHTTP"); }
catch (e) {
alart("XMLHttp object could not be created.");
throw new Error("XMLHttp object could not be created.");
}
}
}
}

ajax.Request = function(url, func, isxml, ispost, parameters) {
var xhr = this.CreateXMLHttp();
if (isUndefined(ispost)) ispost = null;
if (ispost) {
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
}
else
xhr.open("GET", url, true);
if (func) {
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (httpSuccess(xhr))
func(isxml && xhr.responseXML ? xhr.responseXML : xhr.responseText)
else
alert("请求的后台页面出错了!");
}
}
}
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");

if (!isUndefined(parameters))
xhr.send(parameters);
else
xhr.send(null)
}

ajax.Get = function(url, func) {
this.Request(url, func, false, false);
}

ajax.Post = function(url, parameters, func) {
this.Request(url, func, false, true, parameters);
}
...全文
133 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
hackerxxw 2010-04-27
  • 打赏
  • 举报
回复
确实是这样! 感谢啊
sjkof 2010-04-27
  • 打赏
  • 举报
回复
你写的基本正确,注意一些细节就正确了
这里的问题主要是Content-Type
你是
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
这样拿到的就是页面,应该用
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
才能拿到需要的数据
hejun8764 2010-04-26
  • 打赏
  • 举报
回复
建议你引用Ajaxpro.2.dll这样的话就不用自己写那麽多的代码 直接调用后台函数传参数 然后前台接受返回值进行随意处理 当然应用AjaxPro.2.dll有几点小的地方需要注意 自己摸索下吧 不行的话再交流


var info="";
info = EvalSystem.IntelDyn.SelectedItemInfo(id,obj.selectedIndex).value;
//其中EvalSystem.IntelDyn.分别是命名空间和类 而SelectedItemInfo()就是后台的方法
passself 2010-04-26
  • 打赏
  • 举报
回复
纯粹帮顶,不了解.net

87,921

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧