ASP.NET & Ajax in Javascript

GET (https://www.w3schools.com/xml/ajax_intro.asp)

[js]
var xhttp = new XMLHttpRequest();
xhttp.open(“GET”, “ajax_info.txt”, true);
xhttp.send();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert(this.responseText);
}
};
[/js]

POST

Method 1

[js]
// Set up parameters
var strParams = “”;
strParams += “?param1=” + “abcd”;
strParams += “&param2=” + “efgh”;

// Do a post
var xhttp = new XMLHttpRequest();
xhttp.open(“POST”, “DLLName.reliability” + strParams, true);
xhttp.send();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert(this.responseText);
}
};
[/js]

Method 2

[js]
var strParams = “”;
strParams += “?param1=” + “abcd”;
strParams += “&param2=” + “efgh”;

var url = “DLLName.reliability”;
ajaxWebRequest(url + strParams, returnFnc);

function ajaxWebRequest(url,method)
{
//var newDate=Date();
var rand = Math.random();
rand = rand.toString().substring(rand.toString().indexOf(‘.’)+1);
var webRequest = new Sys.Net.WebRequest();
webRequest.set_url(url+”&noCachedCall=”+rand.toString());//call handler instead of cache
webRequest.add_completed(method);
webRequest.invoke();
}

function returnFnc(result, eventArgs)
{
if (result.get_responseData() !=null)
{
alert(“Successful”);
}

if ((result.get_responseData() == “f”)
|| (result._xmlHttpRequest.status != 200))
{
alert(“Fail”);
}
}
[/js]

Be the first to comment

Leave a Reply

Your email address will not be published.


*