Home > Article > Web Front-end > How does the JS AJAX frontend pass parameters to the function of the background class?
This article mainly introduces the method of passing parameters from the JS AJAX frontend to the function of the background class. There is a good example below. Friends who need it can refer to
to publish the methods of ordinary pages as WebMethod. Accessed as Javascript.
1 The method must be decorated with public static, and the return type is preferably string.
2 Add the [WebMethod] attribute before the method.
3 When accessing the client, use the Post method to interact with Json as data. Otherwise, the entire page of HTML will be returned.
4 When accessed by jQuery, data.d in the callback is the real return content.
5 The access URL is: http://abc.com/abc.aspx/GetTime If there is a public static method of GetTime.
Example:
abc.aspx
[WebMethod] public static string GetTime() { return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); }
----------- ----
Script (call jQuery as an example)
$.ajax({ url:url, method:"post", dataType:"json", contentType:"application/json; charset=UTF-8", success: function(data){ $("#id").html(data.d); //见第3点 } });
For more related articles on how the JS AJAX front-end passes parameters to the functions of the background class, please visit Follow PHP Chinese website!