我正在访问服务器端的方法。唯一的问题是我在 AJAx 方面没有太多经验。我无法从 .cs 方法中检索 ajax 中返回的字符串
$.ajax({
type: 'GET',
url: '/frmGpsMap.aspx?name=load',
dataType: 'text',
success: function(data) {
alert(data.d);
}
});
protected void Page_Load(object sender, EventArgs e)
{
string crrName = Request.QueryString["name"];
if (!String.IsNullOrEmpty(crrName))
{
if (crrName.ToLower().Equals("load"))
{
string fh= loadKMLFileToString();
hdnUsername.Value = fh;
}
}
}
public string loadKMLFileToString()
{
return "hello world";
}
警报正在返回页面的 html。我想显示“Hello World”字符串
最佳答案
要使代码隐藏方法与 ajax 一起工作,您需要使用 System.Web.Services.WebMethod。默认情况下你需要使用 POST 除非你在代码后面指定 HTTP GET 属性
[System.Web.Services.WebMethod]
public static string LoadKMLFileToString()
{
return "Hello World";
}
这里是调用的ajax方法
$.ajax({
type: "POST",
url: "frmGpsMap.aspx/LoadKMLFileToString",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
alert(response.d),
failure: function(response) {
alert(response.d);
}
});
希望对你有所帮助。 更多示例:http://weblogs.asp.net/karan/calling-server-side-method-using-jquery-ajax
关于javascript - 将字符串从后面的代码传递给ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30552019/