【问题标题】:Passing JSON object and Javascript Variable through AJAX通过 AJAX 传递 JSON 对象和 Javascript 变量
【发布时间】:2015-07-06 07:49:59
【问题描述】:

我试图在同一个 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);
    }
});

控制器

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

【问题讨论】:

  • 尝试对您发送的整个数据进行字符串化:JSON.stringify({JSONdata:obj,weeks:weeksVar,facilities:facilitiesValue }),
  • @william.taylor.09 好的,只要传递数据就可以了。我将如何处理另一端?
  • @william.taylor.09 很抱歉看起来很暗淡,但这会反序列化为什么数据类型?
  • @william.taylor.09 不用担心,我创建了一个模型来保存各种数据类型。感谢您的帮助

标签: javascript ajax json asp.net-mvc model-view-controller


【解决方案1】:

在您的 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);
}

});

【讨论】:

    猜你喜欢
    • 2013-03-05
    • 1970-01-01
    • 2018-09-10
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 2011-10-19
    相关资源
    最近更新 更多