JQuery Ajax 发布到 C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10653637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery Ajax Post to C#
提问by Kaner TUNCEL
I'm trying to retrieve JSON Object on C# here is my JavasSciprt post but I'm unable to hande it on codebehind, thanks!
我正在尝试在 C# 上检索 JSON 对象,这是我的 JavasSciprt 帖子,但我无法在代码隐藏中处理它,谢谢!
$.ajax({
type: "POST",
url: "facebook/addfriends.aspx",
data: { "data": response.data },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
location = '/facebook/login?URL=' + ReturnURL + '&UID=' + response.authResponse.userID + '&TK=' + response.authResponse.accessToken + '';
}
});
I've tried to retrieve data like:
我试图检索数据,如:
Request.Form["data"]
Request["data"]
采纳答案by jrummell
Here's an example from Encosia.com(I added a form parameter). You don't need to access Page.Form- you can use method parameters instead.
这是来自Encosia.com的示例(我添加了一个表单参数)。您不需要访问Page.Form- 您可以改用方法参数。
Codebehind
代码隐藏
public partial class _Default : Page
{
[WebMethod]
public static string GetDate(string someParameter)
{
return DateTime.Now.ToString();
}
}
Javascript
Javascript
$(document).ready(function() {
// Add the page method call as an onclick handler for the div.
$("#Result").click(function() {
$.ajax({
type: "POST",
url: "Default.aspx/GetDate",
data: {someParameter: "some value"},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Replace the div's content with the page method's return.
$("#Result").text(msg.d);
}
});
});
});
回答by Esen
This is how I did and it worked for me:
这就是我所做的并且对我有用:
$.ajax({
type: "POST",
url: "facebook/addfriends.aspx",
data: "data=" + response.data + "&data1=anyothervaluelikethis",
contentType: "application/x-www-form-urlencoded",
dataType: "json",
success: function (msg) {
location = '/facebook/login?URL=' + ReturnURL + '&UID=' + response.authResponse.userID + '&TK=' + response.authResponse.accessToken + '';
}
});
These two lines are modified
修改了这两行
data: "data=" + response.data + "&data1=anyothervaluelikethis",
contentType: "application/x-www-form-urlencoded",
回答by axl g
The codebehind C# method signature should look something like:
代码隐藏 C# 方法签名应如下所示:
[WebInvoke(UriTemplate = "MyMethod", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
public Object MyMethod(Object data){
// your code
}
where Object can be any serializable class
其中 Object 可以是任何可序列化的类

