我的脚本代码:
$('#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#背后的代码中获取当前时间。
答案 0 :(得分:1)
您需要返回json结果,如下所示:
return JsonConvert.SerializeObject("The Current Time is: "+ DateTime.Now.ToString());
还将以下属性放在上面的方法:
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
当你指定json格式时,你应该写:
contentType: "application/json; charset=utf-8",
顺便说一下,你应该在这里使用Web服务!
答案 1 :(得分:0)
我想设置一个json
内容类型应该这样做:
contentType: 'application/json',
答案 2 :(得分:0)
如果您使用的是vs2013,请确保在route.config中禁用以下行以使其正常工作。
'settings.AutoRedirectMode = RedirectMode.Permanent
我的VB代码背后:
<WebMethod()>
Public Shared Function GetReport(ByVal Data As String) As String
Try
Return "Hello" + Data
Catch ex As Exception
Return "Failed"
End Try
End Function
Js脚本:
$('#btnSave').click(function () {
var char = $(this).text();
var SendData = {};
$.ajax({
type: "POST",
url: "TEST.aspx/GetReport",
data: JSON.stringify(SendData),
data: "{ 'Data': '" + char + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
$('#lbl_test').text(data.d);
},
error: function (data, success, error) {
alert("Error:" + error);
}
});
});