1

a)我正在尝试将存储在变量“mem_ID”中的值从我的 javascript 页面发送到我的服务器端...default.aspx 到我的服务器端 - default.aspx.cs 页面。但我不断收到一条错误消息。

$.ajax({ 

        type: "POST", 
        url: "default.aspx.cs",
        data: "{mem_ID : ' " + mem_ID + "'}",
        async: true,
        // success: function (result) { } 
        });

$ - 未定义。预期的标识符或字符串。

b 部分)此外,一旦我将其发送到服务器端,我如何接收存储在 mem_ID 中的值?

4

1 回答 1

3

你可以使用一个PageMethod. 让我们在后面的代码中举例说明这种方法:

[WebMethod]
public static string MyMethod(string memId)
{
    return string.Format("Thanks for calling me with id: " + memId);
}

注意事项:方法必须是静态的并用[WebMethod]属性修饰。

在客户端,您可以使用如下函数调用此方法jQuery.ajax()

$.ajax({ 
    url: 'default.aspx/MyMethod',
    type: 'POST', 
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({ memID : mem_ID }),
    success: function (result) { 
        alert(result.d);
    } 
});

Also the error you are getting about the undefined $ symbol is related to the fact that you didn't reference the jQuery library in your page. So make sure that in your WebForm you have actually added reference to the jQuery library before using it. For example:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript"></script>
于 2013-02-04T21:43:11.953 回答