我得到了AJAX发送/接收周期来发送字符串代码并接收一个复杂的Json对象,然后我可以做很多事情。在将更复杂的数据发送回服务器方面,我已经阅读了很多关于这个主题的帖子,并且无法弄清楚如何实现这一点。
所以我构建了以下内容,希望得到一些反馈。在以前的版本中,我让它将一个复杂的对象推送到控制器,但在C#中没有足够的结构来使用它。所以我移动了一个固定的结构(命令代码加上一对键值对)。但是,在此版本中,它在ajax发送时失败。
的JavaScript
//=============================
KeyValueObject = new function () {
this.key
this.value
}
//=============================
AjaxSendObject = new function () {
this.code
this.data
}
/
/=============================
function JSTest() {
wrkData = BuildAjaxSendKV()
AJAX_Client_Sender("datasendtest",wrkData)
}
//=============================
function BuildAjaxSendKV() { // build an array of key value objects
wrkAR = []
wrkKV = KeyValueObject
wrkKV.key = "name"
wrkKV.value = "XYZ"
wrkAR.push(wrkKV)
wrkKV = KeyValueObject
wrkKV.key = "title"
wrkKV.value = "ABC"
wrkAR.push(wrkKV)
return wrkAR
}
//=============================
function AJAX_Client_Sender(ajaxRequestType,varSendKV) {
$.ajaxSetup({ cache: false });
var wrkjson = varSendKV
var wrkData = CreateJsonSendData(ajaxRequestType, varSendKV)
a=1
$.ajax({ // fails here, doesn't like the data
type:"POST",
url: "/Main/AJAX_Router",
data: {receiver:wrkData},
datatype: 'json',
success: function (msg) {
if (msg.indexOf("ok") == -1) {
var json = $.parseJSON(msg)
wrkjson = json
}
//var a = json["1"].MainText
AjaxClientRouter(ajaxRequestType, wrkjson)
},
error: function (request, status, error) {
alert(request.responseText);
}
})
}
//=============================
function CreateAJAXSendData(ajaxRequestType, varSendKV)
{
var wrkSend = AjaxSendObject
wrkSend.code = ajaxRequestType
wrkSend.data = varSendKV
return wrkSend
}
C#
//=============================
public class AJAXReceiverWrapper
{
public AJAXReceiver receiver { get; set; }
}
//=============================
public class AJAXReceiver
{
public string code { get; set; }
public string[] ar { get; set; }
}
//=============================
public class MainController : Controller
{
//
public class MainController : Controller
{
public ActionResult AJAX_Router(AJAXReceiverWrapper varReceiverWrapper)
{
string code = "";
string wrkObj = "";
try
{
AJAXReceiver wrkReceiver = varReceiverWrapper.receiver;
code = wrkReceiver.code;
}
catch (Exception e)
{
string errmsg = e.Message;
}
// do more stuff
答案 0 :(得分:2)
感谢Prekak K我有这个工作:
我让这个工作,因为只有几个建议我认为我会发布解决方案。再加上原始代码就是这样一个可能不应该首先发布的kludge。
唯一真正困难的部分(除了仔细编写代码之外!)是获取复杂类型以匹配从客户端发送的对象的结构,但这依赖于后新手对JavaScript和C#的理解(或者在我的案例,反复试验......)
的JavaScript
//=============================
KeyValueObject = new function () {
this.key
this.value
}
//=============================
AjaxSendObject = new function () {
this.code
this.data
}
/
/=============================
function JSTest() {
wrkData = BuildAjaxSendKV()
AJAX_Client_Sender("datasendtest",wrkData)
}
//=============================
function BuildAjaxSendKV() { // build an array of key value objects
wrkAR = []
wrkKV = KeyValueObject
wrkKV.key = "name"
wrkKV.value = "XYZ"
wrkAR.push(wrkKV)
wrkKV = KeyValueObject
wrkKV.key = "title"
wrkKV.value = "ABC"
wrkAR.push(wrkKV)
return wrkAR
}
//=============================
function AJAX_Client_Sender(ajaxRequestType,varSendKV) {
$.ajaxSetup({ cache: false });
var wrkjson = varSendKV
var wrkData = CreateJsonSendData(ajaxRequestType, varSendKV)
wrkDataString = JSON.stringify(wrkData)
$.ajax({
type:"POST",
url: "/Main/AJAX_Router",
data: {jsonString,wrkDataString },
datatype: 'json',
success: function (msg) {
if (msg.indexOf("ok") == -1) {
var json = $.parseJSON(msg)
wrkjson = json
}
//var a = json["1"].MainText
AjaxClientRouter(ajaxRequestType, wrkjson)
},
error: function (request, status, error) {
alert(request.responseText);
}
})
}
//=============================
function CreateAJAXSendData(ajaxRequestType, varSendKV)
{
var wrkSend = AjaxSendObject
wrkSend.code = ajaxRequestType
wrkSend.data = varSendKV
return wrkSend
}
C#
//=============================
public class AJAXReceiver
{
public string code { get; set; }
public List<KeyValueObject> data { get; set; }
}
//=============================
public class KeyValueObject
{
public string key { get; set; }
public string value { get; set; }
}
//=============================
//
public ActionResult AJAX_Router(string jsonString)
{
string code = "";
var varJSSerializer = new JavaScriptSerializer();
List<KeyValueObject> wrkKVL = new List<KeyValueObject>();
try
{
var varJSSerializedResult = varJSSerializer.Deserialize<AJAXReceiver>(jsonString);
code = varJSSerializedResult .code;
wrkKVL = varJSSerializedResult .data;
}
catch (Exception e)
{
string errmsg = e.Message;
}
//--------------------------------------------
switch (code)
// do more stuff