我遇到了一些问题,无法将JavaScript对象数组发布到C# Codebehind。 我按照一个简单的教程进行操作,认为这应该能很好地工作,但我的C# Codebehind中
我尝试将URL更改为“Default.aspx / PassThings”,但它仍然没有发布到我的后端代码,错误警报显示“[object object"]”
以下是我的客户端代码:
Default.aspx
脚本
PassThings
的断点从未被激活。我尝试将URL更改为“Default.aspx / PassThings”,但它仍然没有发布到我的后端代码,错误警报显示“[object object"]”
以下是我的客户端代码:
Default.aspx
脚本
<script>
function Save() {
$(document).ready(function () {
var things = [
{ id: 1, color: 'yellow' },
{ id: 2, color: 'blue' },
{ id: 3, color: 'red' }
];
things = JSON.stringify({ 'things': things });
$.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: 'POST',
url: '/PassThings',
data: things,
success: function () {
alert("success");
},
error: function (response) {
alert(response);
}
});
});
}
</script>
Html
<input type="button" value="Pass Things" onclick="JavaScript: Save();">
Default.aspx.cs
Codebehind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Services;
[System.Web.Script.Services.ScriptService]
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public void PassThings(List<Thing> things)
{
var t = things;
}
public class Thing
{
public int Id { get; set; }
public string Color { get; set; }
}
}
有人看到我做错了什么吗?
谢谢你的时间。
PassThings
函数的Web方法。 - SatpaljqXHR jqXHR, String textStatus, String errorThrown
。所以你目前看到的是 jqXHR 对象。但你可能更关心的是 errorThrown。 - Dan