问题描述
我已经为此工作了3个小时并放弃了。
我只是想使用jQuery将数据发送到asp.net web方法。
数据基本上是一堆键/值对。所以我试图创建一个数组并将对添加到该数组。
I've been working on this for 3 hours and have given up.i am simply trying to send data to an asp.net web method, using jQuery.The data is basically a bunch of key/value pairs. so i've tried to create an array and adding the pairs to that array.
我的WebMethod(aspx.cs)看起来像这样(这对我来说可能是错的我在javascript中构建,我只是不知道):
My WebMethod(aspx.cs) looks like this (this may be wrong for what i'm building in javascript, i just dont know):
[WebMethod]
public static string SaveRecord(List<object> items)
.....
这是我的示例javascript:
Here is my sample javascript:
var data1 = { compId: "1", formId: "531" };
var data2 = { compId: "2", formId: "77" };
var data3 = { compId: "3", formId: "99" };
var data4 = { status: "2", statusId: "8" };
var data5 = { name: "Value", value: "myValue" };
items[0] = data1;
items[1] = data2;
items[2] = data3;
items[3] = data4;
items[4] = data5;
Here is my jQuery ajax call:
var options = {
error: function(msg) {
alert(msg.d);
},
type: "POST",
url: "PackageList.aspx/SaveRecord",
data: { 'items': items },
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(response) {
var results = response.d;
}
};
jQuery.ajax(options);
我收到错误 - 无效的JSON原语:items。
-
i get the error -Invalid JSON primitive: items.
-
所以...如果我这样做:
so...if i do this:
并设置数据参数如下:
然后我收到此错误:
Cannot convert object of type \u0027System.String\u0027 to type \u0027System.Collections.Generic.List`1[System.Object]\u0027
推荐答案
使用时AJAX.NET我总是使输入参数只是一个普通的旧对象,然后使用javascript反序列化器将其转换为我想要的任何类型。至少可以通过这种方式调试并查看Web方法所接收的对象类型。
When using AJAX.NET I always make the input parameter just a plain old object and then use the javascript deserializer to covert it to whatever type I want. At least that way you can debug and see what type of object the web method in is recieving.
使用jQuery时需要将对象转换为字符串
You need to convert your object to a string when using jQuery
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="sm" runat="server" EnablePageMethods="true">
<Scripts>
<asp:ScriptReference Path="~/js/jquery.js" />
</Scripts>
</asp:ScriptManager>
<div></div>
</form>
</body>
</html>
<script type="text/javascript" language="javascript">
var items = [{ compId: "1", formId: "531" },
{ compId: "2", formId: "77" },
{ compId: "3", formId: "99" },
{ status: "2", statusId: "8" },
{ name: "Value", value: "myValue"}];
//Using Ajax.Net Method
PageMethods.SubmitItems(items,
function(response) { var results = response.d; },
function(msg) { alert(msg.d) },
null);
//using jQuery ajax Method
var options = { error: function(msg) { alert(msg.d); },
type: "POST", url: "WebForm1.aspx/SubmitItems",
data: {"items":items.toString()}, // array to string fixes it *
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(response) { var results = response.d; } };
jQuery.ajax(options);
</script>
代码背后
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CustomEquip
{
[ScriptService]
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static void SubmitItems(object items)
{
//break point here
List<object> lstItems = new JavaScriptSerializer().ConvertToType<List<object>>(items);
}
}
}
这篇关于使用jQuery将JSON对象成功发送到asp.net WebMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!