日期:2014-05-16  浏览次数:20405 次

javascript ajax访问.net静态方法??
jquery的 ajax方法访问 .net静态方法
1、c# 静态方法
C# code

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


2、jquery方法 可以正确返回
JScript code

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


3、javascript 自己写的xmlhttprequest访问不了静态方法
希望了解这方面的大侠 指点一下 啊
JScript code
 
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);
&n