0

可能重复:
如何使用 jquery/ajax 发布到表单

如何通过 Jquery Ajax 将数据发布到服务器?

jQuery

function postNewBaseLine() {
    var id = "300";
    $.ajax({
            type: "POST",
            url: "ManagerBaseKit.aspx/SetNewBaseVersion",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: id,
            success: function(data) {
                alert('success!');
            }
        });
}

CS

[WebMethod]
public static void SetNewBaseVersion(string version)
{
    // I want it here!
}

我正在使用脚本管理器

<asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" EnablePartialRendering="true"
    runat="server" />

编辑: 更改为data: { 'version': id },

我收到 POST _http://localhost:49852/ManagerBaseKit.aspx/SetNewBaseVersion 500(内部服务器错误)

4

2 回答 2

2

您需要将数据对象更改为:

function postNewBaseLine() {
    var id = "300";
    $.ajax({
            type: "POST",
            url: "ManagerBaseKit.aspx/SetNewBaseVersion",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: {version: id},
            success: function(data) {
                alert('success!');
            }
        });
}
于 2012-10-09T15:13:49.040 回答
1

首先将 ID 更改为对象,我相信引号很重要。

function postNewBaseLine() {
var id = "300";
$.ajax({
        type: "POST",
        url: "ManagerBaseKit.aspx/SetNewBaseVersion",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: {"version": id},
        success: function(data) {
            alert('success!');
        }
    });

}

您可能还需要在服务方法中添加一些属性

          [WebMethod]
          [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, bodystyle = WebMessageBodyStyle.WrappedRequest)]
            public static void SetNewBaseVersion(string version)
            {
                // I want it here!
            }
于 2012-10-09T15:18:27.913 回答