在前端使用jQuery AJAX发送数据到后端C#时,可以通过以下代码示例解决问题。
前端代码:
$.ajax({
type: "POST",
url: "your-backend-url",
data: { param1: "value1", param2: "value2" },
dataType: "json",
success: function (response) {
// 处理从后端返回的响应
},
error: function (jqXHR, textStatus, errorThrown) {
// 处理发生错误时的情况
}
});
在后端C#中,需要使用WebRequest或HttpClient来接收前端发送的数据。下面是使用WebRequest的示例:
后端代码:
using System.Net;
using System.Web;
public void HandleRequest(HttpContext context)
{
var request = context.Request;
string postData = string.Empty;
using (var reader = new System.IO.StreamReader(request.InputStream))
{
postData = reader.ReadToEnd();
}
// 处理postData,即为前端发送的数据
}
下一篇:AJAX简介