I am trying to make JavaScript work with C#. For now, i am just trying to retrieve (GET) a return result from the C# and display it via JavaScript. Later, it will be for database writing (POST). Like so, and after reading, here's where i got stuck:
I have the button:
<button id="btn" onclick="Create();">CREATE</button>
Then JS code:
function Create() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (xhttp.readyState == 4 && xhttp.status == 200) {
alert(xhttp.response)
}
};
xhttp.open("GET", "default.aspx/Create", true);
xhttp.send();
}
Then the C# WebMethod:
[WebMethod]
public static string Create()
{
return "WebMethod";
}
So, how can i get the "WebMethod" value on "onreadystatechange"? And make all data JSON? Don't need to code for me, just point me in the right direction, as where i'm failing on the concept, as i read many conflicting opinions on the correct way. No jQuery.