ASP.NET/JavaScript - Ajax call, how?

Please be careful, as I'm still new to web programming and -very-new for Ajax!

I created a C # function that retrieves data from the mssql database, formats it into a json string, and returns it. Now I need to make a call from my javascript (jQuery) slider through an aspx page that is linked to a C # code file.

I actually never did anything like this before, from what I could say on googling, I need to use xmlHttpRequest, but how exactly can I get this function to get this string?

It would be great if someone had some sample code that shows how this works.

+3
source share
2

jQuery, # [WebMethod] - ASP.NET. , JSON ASP.NET, (IMHO).

, WebMethod GetData, URL-, .

$.ajax({ url: "somepage.aspx/GetData", 
         method: "POST", // post is safer, but could also be GET
         data: {}, // any data (as a JSON object) you want to pass to the method
         success: function() { alert('We did it!'); }
});

:

[WebMethod]
public static object GetData() {
    // query the data...
    // return as an anonymous object, which ASP.NET converts to JSON
    return new { result = ... };
}
+1

- ASHX, JSON HTTP.

XmlHttpRequest, jQuery.

jQuery :

$.get("/YourFile.ashx", function(obj) { ... }, "json");
+2

Source: https://habr.com/ru/post/1734887/


All Articles