gpt4 book ai didi

javascript - 通过 AJAX 传递 JSON 对象和 Javascript 变量

转载 作者:行者123 更新时间:2023-11-28 07:24:08 24 4
gpt4 key购买 nike

我试图在同一个 AJAX 调用中传递一个 JSON 对象和 2 个 JavaScript 数组。

我不断收到此错误

    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);
}
});

Controller

     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();
}

最佳答案

尝试在 ajax 调用中使用传统: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);
}

});

关于javascript - 通过 AJAX 传递 JSON 对象和 Javascript 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29902861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com