我有一个ajax函数,其中我要发送两个这样的字符串化对象:
function SaveOffsetOrder() {
var temp = sessionStorage.getItem('book');
var viewName = $.parseJSON(temp);
var BookObj = JSON.stringify({ obj: viewName })
var OffsetCommonModel = {
ProductId: $('#OffProductId').val(),
CustomerId: $("#OffCustomerId").val(),
}
var OffsetCommonObj = JSON.stringify({ 'OffsetCommonObj':
OffsetCommonModel });
$.ajax({
contentType: "application/json; charset=utf-8",
type: "Post",
url: "@Url.Content("~/Estimate/CreateOffset")",
data: OffsetCommonObj + '&' + $.param(BookObj),
dataType: 'json',
success: function (data) {
}
});
这是我的操作方法:-
public ActionResult CreateOffset(OffsetCommonModel OffsetCommonObj, CalculationModel obj)
{
// do something with objects
}
但是当我在控制台中检查其给出的错误“无效的JSON原语”时 我在哪里做错了,请帮忙..谢谢
答案 0 :(得分:0)
您在ajax中的数据部分应该是这样的,无需单独对对象进行字符串化,只需执行一次
function SaveOffsetOrder() {
var temp = sessionStorage.getItem('book');
var viewName = $.parseJSON(temp);
// var BookObj = { obj: viewName }
var OffsetCommonModel = {
ProductId: $('#OffProductId').val(),
CustomerId: $("#OffCustomerId").val(),
}
// var OffsetCommonObj = { 'OffsetCommonObj': OffsetCommonModel };
$.ajax({
contentType: "application/json; charset=utf-8",
type: "Post",
url: "@Url.Content("~/Estimate/CreateOffset")",
data: JSON.stringify({ 'OffsetCommonObj': OffsetCommonModel, 'obj': viewName }),
dataType: 'json',
success: function (data) {
}
});
}