如何接受一个.aspx页面方法的JSON字符串数组 [英] How to accept a JSON string array in an .aspx Page method
问题描述
我使用jQuery AJAX传递一个字符串数组到一个页面的方法。我需要知道如何处理这些数据做一旦获得该页面的方法。我的目标是将其插入到一个数据库,但在此之前,我需要做阵列感。由于它是,它是过来是这样的:{数据:theArray},它关联到{数据:1,2,3,4,5,6}。但在底层数据库4,5和6重新present另一行,因此将7,8和9,等等,等等。我是新来的Ajax。
下面是jQuery的:
对提交给服务器//包装表数据
$(#saveToDB)。点击(函数(){
VAR dataForSubmit =新的Array();
//收集的所有数据阵列除删除单元,.rowToDelete
$('#QueueTable TBODY TD:不(.rowToDelete)')每个(函数(){。
dataForSubmit.push($(本)的.html());
});
//发送阵列的方法
callScriptMethod('DailyReceipts.aspx / SAVEDATA',{海图:dataForSubmit});
}); 功能callScriptMethod(URL,JSONObject的回调,异步){ 回调回调= ||功能(){};
异步=(异步== NULL ||异步); $阿贾克斯({
键入:POST,
的contentType:应用/ JSON的;字符集= UTF-8,
网址:网址,
数据:JSON.stringify(的JSONObject)
数据类型:JSON
异步:异步,
成功:函数(jsonResult){
如果(在jsonResult'D')
回调(jsonResult.d);
其他
回调(jsonResult);
},
错误:函数(){
警报(错误调用'+ URL +'+ JSON.stringify(JSONObject的));
回电话([]);
}
});
}
这里是页面的方法,做静态和作为[的WebMethod]:
[的WebMethod]
公共静态无效SAVEDATA(字符串[]海图)
{ //遍历数组
的for(int i = 0; I< theData.Length;我++)
{ } }
<一个href=\"http://www.dexign.net/post/2008/07/16/jQuery-To-Call-ASPNET-Page-Methods-and-Web-Services.aspx\"相对=nofollow>这里是关于调用使用jQuery .aspx页面中方法的文章。
你可以做的是利用这些信息在那篇文章中使用参数来发布JSON格式的字符串的方法,那么使用的 JSON.NET 字符串解析为一个对象。
I am using jQuery ajax to pass a string array to a page method. I need to know what to do with that data once it gets to that page method. My goal is to insert it into a DB but before that I need to make sense of the array. As it is, it is coming over like this: { data : theArray} which correlates to {data: 1,2,3,4,5,6}. But 4,5, and 6 represent another row in the underlying DB and so would 7,8 and 9, and so on and so forth. I am new to Ajax.
Here is the jQuery:
//packaging table data for submit to server
$("#saveToDB").click(function() {
var dataForSubmit = new Array();
//gather all data to array except the "delete" cell, .rowToDelete
$('#QueueTable tbody td:not(.rowToDelete)').each(function() {
dataForSubmit.push($(this).html());
});
//send array to method
callScriptMethod('DailyReceipts.aspx/saveData', { theData: dataForSubmit });
});
function callScriptMethod(url, jsonObject, callback, async) {
callback = callback || function() { };
async = (async == null || async);
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: url,
data: JSON.stringify(jsonObject),
dataType: "json",
async: async,
success: function(jsonResult) {
if ('d' in jsonResult)
callback(jsonResult.d);
else
callback(jsonResult);
},
error: function() {
alert("Error calling '" + url + "' " + JSON.stringify(jsonObject));
callback([]);
}
});
}
And here is the page method, made static and as a [WebMethod]:
[WebMethod]
public static void saveData(string[] theData)
{
//iterate the array
for (int i = 0; i < theData.Length; i++)
{
}
}
Here is an article on calling .aspx page method with jQuery.
What you can do is use the information in that article to post a JSON-formatted string to the method using a parameter, then use JSON.NET to parse the string into an object.
这篇关于如何接受一个.aspx页面方法的JSON字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!