我的脚本代码:
$('#btnSave').click(function() {
var pageUrl = '<%= ResolveUrl("~/TestPage.aspx/SystemEdit")%>';
var ip = $('#editIP').text();
var loc = $('#txtBay').val();
var team = $('#txtTeam').val();
var port = $('#txtPort').val();
var xcel = "", office = "", moni = "";
var parameter={ "ip": ip, "loc": loc, "team": team, "port": port, "excel": xcel, "office": office, "monitor": moni}
$.ajax({
type: 'POST',
url: pageUrl,
data: JSON.stringify(parameter),
contentType: 'json',
success: function(data) {
alert(data);
},
error: function(data,success,error) {
alert("Error:" +error);
}
});
});
我的 C# 代码背后的代码是:
[WebMethod]
public static string SystemEdit(string ip, string loc,string team, string port, string excel,string office, string monitor)
{
return "The Current Time is: "+ DateTime.Now.ToString();
}
我的页面名称是:TestPage.aspx
点击保存按钮时,我得到“未定义”。我没有从 c# 背后的代码获取当前时间。
最佳答案
需要返回如下json结果:
return JsonConvert.SerializeObject("The Current Time is: "+ DateTime.Now.ToString());
也把下面的属性放在方法上面:
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
当你指定 json 格式时,你应该写:
contentType: "application/json; charset=utf-8",
顺便说一句,你应该在这里使用 Web 服务!
关于c# - jQuery,ajax POST 方法成功返回 : Undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32330697/