【发布时间】:2015-01-29 03:34:40
【问题描述】:
我想在函数 test() 中将我的席位数传递给 TryJSIN.aspx。 但我有错误,当我使用“GET”类型时,我有错误
“无效的 JSON 原语:1-9。”
1-9 是 noSeat 值。 但是当我更改为“POST”时出现错误
尝试使用 POST 请求调用方法“test”,这是不允许的。
请检查我的以下代码。这是我使用get type的时候
var sid = jQuery(this).attr('id');
$.ajax({
url: "TryJSON.aspx/test",
type: "GET",
data: {noSeat: sid},
contentType: "application/json; charset=utf-8",
success: function (response) {
// var arr = JSON.parse(response.d);
console.log(arr);
},
error: function () {
alert("sorry, there was a problem!");
console.log("error");
},
complete: function () {
console.log("completed");
}
});
当我使用 POST 类型时,下面的 ajax
var sid = jQuery(this).attr('id');
$.ajax({
url: "TryJSON.aspx/test",
type: "POST",
data: JSON.stringify({'noSeat': sid}),
contentType: "application/json; charset=utf-8",
success: function (response) {
// var arr = JSON.parse(response.d);
console.log(arr);
},
error: function () {
alert("sorry, there was a problem!");
console.log("error");
},
complete: function () {
console.log("completed");
}
});
这是我的 C# 代码
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static string test(string noSeat)
{
return noSeat;
}
请帮助我。我是 c# ajax 和 jQuery 的新手。所以我需要很多帮助
更新: 我在第一次评论后编辑我的代码。但它显示相同。
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static string test(string noSeat)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(noSeat).ToString();
}
【问题讨论】:
-
你没有返回 JSON。
-
我曾经使用过 json 序列化程序,但它仍然与 @SLaks 相同的注释出错
-
您在开发工具中看到什么响应正文?
标签: c# jquery ajax json asp.net-ajax