如何通过JQuery传递数据而不使用post
我的jquery代码是
$('[name="SelectHighlights"]:checked').each(function () {
var row = $(this).closest('tr');
var high = {
AccountName: row.find('td:nth-child(2)').text(),
Highcomments: row.find('td:nth-child(3) > input').val()
};
HighlightsArea.push(high);
});
var HL=HighlightsArea.length;
alert(''+HL);
if(HL>0)
{
GetJson(
GetRootPath() + '/WeeklySales/AjaxUpdateHighComments', //url
//JSON.stringify(HighlightsArea), //Data:
//HighlightsArea,
//{high: HighlightsArea},
{array: HighlightsArea.join(",")},
SaveChanges //callBack
);
}
GetJSon的功能:
function GetJson(url, data, onSuccess) {
$.ajax({
url: url,
data: data,
success: onSuccess,
error: GenericErrorHandler,
traditional: true,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: 'GET'
});
}
我控制器中的代码:
我想将该数组仅传递给此控制器,但我不应该使用HTTpPost
public ActionResult AjaxUpdateHighComments(List<Highlights> Highlights, WeeklySalesModel weeklysales){}
请帮帮我 任何形式的帮助都非常感谢。 提前谢谢。
答案 0 :(得分:1)
我建议您必须首先在传递数据时将字符串转换为字符串。
客户端
function GetJson(url, data, onSuccess) {
$.ajax({
url: url,
data: JSON.stringify(data), // converted to string
success: onSuccess,
error: GenericErrorHandler,
traditional: true,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: 'GET'
});
}
尝试使用此代码
控制器
[HttpGet]
public ActionResult AjaxUpdateHighComments(string highlights)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
List<Highlights> listHighlights = jss.Deserialize<List<Highlights>>(highlights);
...
答案 1 :(得分:0)
你的问题有点含糊不清但我会试一试,假设你的意思是你想阻止发布到行动中?
您可以在操作上添加属性以阻止此MSDN HttpGetAttribute
例如
[HttpGet]
public ActionResult AjaxUpdateHighComments(List<Highlights> Highlights, WeeklySalesModel weeklysales){}
答案 2 :(得分:0)
首先,请添加this article中的$.postify
代码。
$.postify
的代码:
$.postify = function(value) {
var result = {};
var buildResult = function(object, prefix) {
for (var key in object) {
var postKey = isFinite(key)
? (prefix != "" ? prefix : "") + "[" + key + "]"
: (prefix != "" ? prefix + "." : "") + key;
switch (typeof (object[key])) {
case "number": case "string": case "boolean":
result[postKey] = object[key];
break;
case "object":
if (object[key].toUTCString)
result[postKey] = object[key].toUTCString().replace("UTC", "GMT");
else {
buildResult(object[key], postKey != "" ? postKey : key);
}
}
}
};
buildResult(value, "");
return result;
};
我知道这个名称听起来像POST
,但它也适用于GET
。然后更改您的代码如下:
$('[name="SelectHighlights"]:checked').each(function () {
var row = $(this).closest('tr');
var high = {
AccountName: row.find('td:nth-child(2)').text(),
Highcomments: row.find('td:nth-child(3) > input').val()
};
HighlightsArea.push(high);
});
var HL = HighlightsArea.length;
alert('' + HL);
if (HL > 0) {
//Property names should match the parameters' name given in the controller method
var objRequest = {
Highlights: HighlightsArea,
weeklysales: {/* weekly sales object goes here */}
};
GetJson(
GetRootPath() + '/WeeklySales/AjaxUpdateHighComments',
$.postify(objRequest), // $.postify is the key
SaveChanges //callBack
);
}