使用Ajax传递多个参数

时间:2014-06-24 15:41:34

标签: asp.net ajax

我的.aspx页面中有一个函数,我从用户那里收集名字和姓氏,并希望它传回服务器端代码(c#)。正在使用Ajax,并且在传递一个参数时运行正常,但是当传递第二个参数时,我收到一个错误:有人可以轻松一点,谢谢

ERROR:

Failed to load resource: the server responded with a status of 500 (Internal Server Error) 

POST http://ingleParam.aspx/GetCurrentTime 500 (Internal Server Error) jquery-.11.1.min.js:4
send jquery-1.11.1.min.js:4
m.extend.ajax jquery-1.11.1.min.js:4
ShowCurrentTime SingleParam.aspx:12
onclick

.ASPX代码

function ShowCurrentTime() {
    $.ajax({
        type: "Post",
        url: "SingleParam.aspx/GetCurrentTime",
        data: {     name: $("#<%=txtUserName.ClientID%>").val(), 
                    lastName: $("#<%=txtLastName.ClientID%>").val() },
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function (response) {
            alert(response.d);
        }
    });
}

function OnSuccess(response) {
    alert(response.d);
}

服务器端代码:

[WebMethod]
public static string GetCurrentTime(string name, string lastname) {
    return "Hello " + name + Environment.NewLine + "The current time is " + DateTime.Now.ToString();
}

1 个答案:

答案 0 :(得分:1)

我遇到了将json对象发送到webmethods的问题。当我对发送的数据进行字符串化时,我已经取得了成功。像这样:

data: JSON.stringify({name: $("#<%=txtUserName.ClientID%>").val(), 
                    lastName: $("#<%=txtLastName.ClientID%>").val()}),

尝试一下,看看它是否有帮助。