加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – jQuery,ajax POST方法成功返回:未定义

发布时间:2020-12-15 22:19:57 所属栏目:百科 来源:网络整理
导读:我的脚本代码: $('#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 x
我的脚本代码:

$('#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",

顺便说一句,你应该在这里使用Webservice!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读