JQuery TypeError:在MVC中将循环结构转换为JSON

时间:2016-03-09 06:13:50

标签: javascript jquery json

JSON.stringify(ApplObj)面临错误: 如何将此对象发布到控制器 JQuery代码 -

while ($row = mysql_fetch_assoc($sql)) {
  echo '<img src="data:image/jpeg;base64,' . base64_encode($row['category_icon']) . '" width="290" height="290">' ;
}

控制器代码...... 这个jsonresult用于保存记录和返回值... 这段代码在其他项目中工作....

    var ApplName = $("#ApplicantName").val();
    var ApplMobile = $("#ApplicantMobile").val();
    var ApplEmail = $("#ApplicantEmailId").val();
    var ApplFHName = $("#ApplicantFHName").val();

    var ApplObj = {
        ApplicantName: ApplName, ApplicantMobile: ApplMobile, ApplicantEmailId: ApplEmail, ApplFHName: ApplicantFHName
    }

    $.ajax({
        url: '@Url.Action("SaveApplicatDetail", "Dashboard")',
        data: JSON.stringify(ApplObj),
        dataType: 'json',
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            alert(data);
        }
    });

2 个答案:

答案 0 :(得分:0)

基本上JSON.stringify将Javascript对象转换为JSON文本,并将该JSON文本存储在字符串中。

试试这个,

        var ApplName = $("#ApplicantName").val();
        var ApplMobile = $("#ApplicantMobile").val();
        var ApplEmail = $("#ApplicantEmailId").val();
        var ApplFHName = $("#ApplicantFHName").val();

        var ApplObj = {
            'ApplicantName': ApplName, 'ApplicantMobile': ApplMobile, 'ApplicantEmailId': ApplEmail, 'ApplFHName': ApplicantFHName
        }

        $.ajax({
            url: '@Url.Action("SaveApplicatDetail", "Dashboard")',
            data: JSON.stringify(ApplObj),
            dataType: 'json',
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                alert(data);
            }
        });

答案 1 :(得分:0)

问题已解决 - &#34; JSON.stringify&#34;用数组转换它。

    var ApplName = $("#ApplicantName").val();
    var ApplMobile = $("#ApplicantMobile").val();
    var ApplEmail = $("#ApplicantEmailId").val();
    var ApplFHName = $("#ApplicantFHName").val();

    var ApplDetails = {
        ApplicantName: ApplName,
        ApplicantMobile: ApplMobile,
        ApplicantEmailId: ApplEmail,
        ApplicantFHName: ApplFHName
    }

    var ApplObj = { ApplicantDetail: ApplDetails };

    $.ajax({
        url: '@Url.Action("SaveApplicatDetail", "Dashboard")',
        data: JSON.stringify(ApplDetails),
        dataType: 'json',
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        success: function (data) {
        }
    });