这是带有ajax帖子的aspx页面上的一些代码:
<div>
Make Comment: <br />
<textarea rows="7" cols="20" id="comment_content"></textarea><br />
<input type="button" id="Make_Comment" value="Make Comment" />
</div>
<script type="text/javascript">
$(document).ready(function () {
$("#Make_Comment").click(function () {
$.ajax({
type: "POST",
url: "Conversation.aspx/AddComment",
data: '{ comment: This is a test comment via ajax postback }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {
alert("Got Back to code");
}
});
});
});
</script>
这是我试图在服务器端点击的方法:
[WebMethod]
public static void AddComment(string data)
{
}
我已经通过服务器端方法设置了一个断点,但它没有打到它,可能是什么问题?
答案 0 :(得分:2)
一些事情:
您的data
ina ajax
来电
data: '{ comment: This is a test comment via ajax postback }'
应该是
data: "{'comment':'This is a test comment via ajax postback'}",
您的WebMethod
:
AddComment(string data)
应该是
AddComment(string comment)
答案 1 :(得分:1)
试试这个:
<script type="text/javascript">
$(document).ready(function () {
$("#Make_Comment").click(function () {
var comment = $("#comment_content").val();
var Params = { comment : comment };
$.ajax({
type: "POST",
url: "Conversation.aspx/AddComment",
data: Params,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {
alert("Got Back to code");
}
});
});
});
</script>
在您的控制器中设置httppost。
[WebMethod]
[HttpPost]
public static void AddComment(string comment)
{
}