如何通过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(内部服务器错误)
答案 0 :(得分: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!');
}
});
}
答案 1 :(得分: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!
}