0

I would not like to call asp.net server side code with jquery $.ajax . So I have written a pure javascript ajax file .But when I call webmethod,this do not work. Can anyony help me out how correct this? THANK you very much .

ajax.js:

var ajax = {
    _params: null,
_callback: null,
 _xhr: null,
_createXHR: function () {
if (window.ActiveXObject) {
 _xhr = new ActiveXObject("Microsoft.XMLHTTP");     //IE
}
    else if (window.XMLHttpRequest) {
        _xhr = new XMLHttpRequest();      //FireFox,Chrome et.
    }
},

_ajaxcallback: function () {
    if (_xhr.readyState == 4) {
        if (_xhr.status == 200) {
            _callback.call(this, _xhr.responseText)
        }
    }
},

_changeParams: function () {
    var args = arguments[0];
    var s = "";
    for (var i in args) {
        s += "&" + i + "=" + args[i];
    }
    _params = s;
},

get: function (url, params, callback) {
    _callback = callback;
    ajax._createXHR();
    ajax._changeParams(params);
    if (null != _xhr) {
        _xhr.open('get', url + '?' + _params, true);
        _xhr.onreadystatechange = ajax._ajaxcallback;
        _xhr.send();
    }
},

post: function (url, params, callback) {
    _callback = callback;
    ajax._createXHR();
    ajax._changeParams(params);
    if (null != _xhr) {
        _xhr.open('post', url, true);
        _xhr.onreadystatechange = ajax._ajaxcallback;
        _xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        _xhr.send(_params);
    }
}
}

WebForm1.aspx

<head runat="server">
<title></title>
<script src="ajax.js" type="text/javascript"></script>
<script type="text/javascript">
    function ajaxtest() {
        var uid = document.getElementById("txtuid").value;
        var pwd = document.getElementById("txtpwd").value;
        ajax.post("WebForm1.aspx/GetModel", "{ 'uid':" + uid + ", 'pwd':" + pwd + " }", function (data) {
            alert(data);
        });
    }
</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="txtuid" value="eeee" />
<input type="text" value="222" id="txtpwd" onblur="ajaxtest()"/>

WebForm1.cs:

     [WebMethod]
    public static string GetModel(string uid,string pwd)
    {

     return "1";
    }

2 Answers 2

3

In your markup you need to have a ScriptManager with EnablePageMethods set to true. Doing this will ensure you can call the methods you have marked up with [WebMethod].

In your JavaScript you can then call your method like this: PageMethods.GetModel("userName", "password", OnSuccessMethod, OnFailureMethod); - you won't need any of the ActiveXObject/XmlHttpRequest stuff if you do it this way, which keeps things much simpler.

Sign up to request clarification or add additional context in comments.

Comments

0

Use AJAX.PRO from Michael Schwarz --> http://www.ajaxpro.info/

1 Comment

Please consider updating your answer. As it stands, it lacks information to make it useful for developers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.