通过AJAX传递JSON对象和Javascript变量
我试图在同一个AJAX调用中传递一个JSON对象和两个javascript数组 我一直收到这个错误通过AJAX传递JSON对象和Javascript变量,javascript,ajax,json,asp.net-mvc,model-view-controller,Javascript,Ajax,Json,Asp.net Mvc,Model View Controller,我试图在同一个AJAX调用中传递一个JSON对象和两个javascript数组 我一直收到这个错误 System.ArgumentException: Invalid JSON primitive: object 我相信这可能与传递的不同类型的变量有关 你能发现任何明显的错误吗 谢谢 var requestData = { "deptCode": userVar, "roundID": parseInt(roundIDVar), "moduleCode"
System.ArgumentException: Invalid JSON primitive: object
我相信这可能与传递的不同类型的变量有关
你能发现任何明显的错误吗
谢谢
var requestData = {
"deptCode": userVar,
"roundID": parseInt(roundIDVar),
"moduleCode": moduleCodeVar,
"priority": parseInt(priorityVar),
"day": parseInt(dayVar),
"start": parseInt(timeVar)-8,
"length": parseInt(lengthVar),
"weeks": weeksNum,
"capacity": parseInt(studentsVar),
"type": roomTypeVar,
"otherReqs": otherReqs
};
var obj = JSON.stringify(requestData);
$.ajax({
type: 'POST',
url: '/create/Submit',
error: function (xhr, ajaxOptions, thrownError) {
alert("Submitting Failed. Please Reload and Try Again.");
},
data: {JSONdata:obj,weeks:weeksVar,facilities:facilitiesValue },
datatype: 'html',
contentType: 'application/json',
processData: false,
async:false,
success: function (data) {
alert(data);
}
});
控制器
public ActionResult Submit(request JSONdata, String[] Weeks, String[] facilities) {
ViewBag.module = JSONdata.weeks;
if(JSONdata.otherReqs==null){
JSONdata.otherReqs = "None";
}
JSONdata.sent = 1;
JSONdata.status = 0;
JSONdata.viewed = 0;
JSONdata.booked = 0;
db.requests.Add(JSONdata);
try
{
// Your code...
// Could also be before try if you know the exception occurs in SaveChanges
db.SaveChanges();
}
catch (DbEntityValidationException e)
{
foreach (var eve in e.EntityValidationErrors)
{
Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name, eve.Entry.State);
foreach (var ve in eve.ValidationErrors)
{
Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName, ve.ErrorMessage);
}
}
throw;
}
if(Convert.ToInt16(JSONdata.weeks)==1){
for (var i = 0; i < Weeks.Length; i++) {
weeks_request newWeek = new weeks_request();
newWeek.week = Convert.ToInt16(Weeks[i]);
newWeek.requestID = JSONdata.requestID;
db.weeks_request.Add(newWeek);
db.SaveChanges();
}
}
return View();
}
public ActionResult提交(请求JSONdata,字符串[]周,字符串[]设施){
ViewBag.module=JSONdata.weeks;
if(JSONdata.otherReqs==null){
JSONdata.otherReqs=“无”;
}
JSONdata.sent=1;
JSONdata.status=0;
JSONdata.view=0;
JSONdata.0=0;
db.requests.Add(JSONdata);
尝试
{
//你的代码。。。
//如果您知道SaveChanges中发生异常,也可以在尝试之前进行
db.SaveChanges();
}
捕获(DbEntityValidationException e)
{
foreach(e.EntityValidationErrors中的变量eve)
{
WriteLine(“状态为“{1}\”的类型为“{0}\”的实体”存在以下验证错误:“,
eve.Entry.Entity.GetType().Name,eve.Entry.State);
foreach(eve.ValidationErrors中的变量ve)
{
Console.WriteLine(“-Property:\“{0}\”,错误:\“{1}\”,
ve.PropertyName,ve.ErrorMessage);
}
}
投掷;
}
如果(转换为16(JSONdata.weeks)==1){
对于(变量i=0;i
在ajax调用中尝试使用traditional:true
$.ajax({
type: 'POST',
url: '/create/Submit',
error: function (xhr, ajaxOptions, thrownError) {
alert("Submitting Failed. Please Reload and Try Again.");
},
data: {JSONdata:obj,weeks:weeksVar,facilities:facilitiesValue },
datatype: 'html',
contentType: 'application/json',
processData: false,
async:false,
traditional: true,
success: function (data) {
alert(data);
}
})) 尝试对发送的整个数据进行字符串化:JSON.stringify({JSONdata:obj,weeks:weeksVar,facilities:facilitiesValue}),@william.taylor.09就传递数据而言,这是有效的。另一方面,我该如何处理这个问题呢?反序列化:@william.taylor.09抱歉,看起来很模糊,但是这个反序列化的数据类型是什么?@william.taylor.09不用担心,我创建了一个模型来保存各种数据类型。谢谢你的帮助