【问题标题】:jQuery Ajax call returning JSON string not as object arrayjQuery Ajax 调用返回 JSON 字符串而不是对象数组
【发布时间】:2019-10-05 18:36:51
【问题描述】:

我正在从数据库中获取数据并将 ASP.NET MVC 视图页面中的数据显示为表格格式。下面是 Ajax 调用。我将返回数据作为 JSON 字符串获取。我不确定为什么。对于所有视图页面,我都以 JSON 对象的形式获取。

虽然值在 JSON 字符串中,但我正在尝试使用“JSON.parse(jsonData)”转换为 JSON 对象。它在我当地运行良好。当我在 Stage 中部署代码时。线路在 Json Parse 中断。

为什么我会收到此错误。代码中有什么工作吗?

JSON 字符串:

[{"SuppNum":null,"Documentumid":"090111738011c814:Supplier Needful Info.xlsx:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet","DocDescription":"Paper Agreement","CreateDate":"05/11/2019","Userid":null,"LastChnageDate":null,"LastChnageUserid":null,"DocTypeCode":"40","AttachmentFilename":"Supplier Needful Info","ExpirtyDate":null,"IsDeleted":null,"DocTypeCodeDescription":"PII Paper Agreement                               ","ContentType":"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},{"SuppNum":null,"Documentumid":"090111738011c2c2:WP_SQL_Query1.pdf:application/pdf","DocDescription":"Expense Report","CreateDate":"05/14/2019","Userid":null,"LastChnageDate":null,"LastChnageUserid":null,"DocTypeCode":"27","AttachmentFilename":"WP_SQL_Query1","ExpirtyDate":null,"IsDeleted":null,"DocTypeCodeDescription":"Expense                                           ","ContentType":"application/pdf"},{"SuppNum":null,"Documentumid":"090111738011c2cc:UiPath-Level1_Certificate.pdf:applicat    ion/pdf","DocDescription":"Paper DOC","CreateDate":"05/14/2019","Userid":null,"LastChnageDate":null,"LastChnageUserid":null,"DocTypeCode":"40","AttachmentFilename":"UiPath-Level1_Certificate","ExpirtyDate":null,"IsDeleted":null,"DocTypeCodeDescription":"PII Paper Agreement                               ","ContentType":"application/pdf"},{"SuppNum":null,"Documentumid":"090111738011c2e9:Communication Plan 865944.docx:application/vnd.openxmlformats-officedocument.wordprocessingml.document","DocDescription":"DOCS","CreateDate":"05/16/2019","Userid":null,"LastChnageDate":null,"LastChnageUserid":null,"DocTypeCode":"1","AttachmentFilename":"Communication Plan 865944","ExpirtyDate":null,"IsDeleted":null,"DocTypeCodeDescription":"Insurance Documents                               ","ContentType":"application/vnd.openxmlformats-officedocument.wordprocessingml.document"},{"SuppNum":null,"Documentumid":"090111738011c2ea:Communication Plan 865944.docx:application/vnd.openxmlformats-officedocument.wordprocessingml.docum    ent","DocDescription":"Amedments","CreateDate":"05/17/2019","Userid":null,"LastChnageDate":null,"LastChnageUserid":null,"DocTypeCode":"20","AttachmentFilename":"Communication Plan 865944","ExpirtyDate":null,"IsDeleted":null,"DocTypeCodeDescription":"Amendment                                         ","ContentType":"application/vnd.openxmlformats-officedocument.wordprocessingml.document"},{"SuppNum":null,"Documentumid":"090111738011c310:How to attach in OSA.pdf:application/pdf","DocDescription":"","CreateDate":"05/18/2019","Userid":null,"LastChnageDate":null,"LastChnageUserid":null,"DocTypeCode":"32","AttachmentFilename":"How to attach in OSA","ExpirtyDate":null,"IsDeleted":null,"DocTypeCodeDescription":"EVP Attachment                                    ","ContentType":"application/pdf"},{"SuppNum":null,"Documentumid":"090111738011c2ec:WP_SQL_Query1.pdf:application/pdf","DocDescription":"","CreateDate":"05/18/2019","Userid":null,"LastChnageDate":null,"LastChnageUserid":null,"DocTypeCode":"27","AttachmentFi    lename":"WP_SQL_Query1","ExpirtyDate":null,"IsDeleted":null,"DocTypeCodeDescription":"Expense                                           ","ContentType":"application/pdf"},{"SuppNum":null,"Documentumid":"090111738011c2ed:SyamOld_Passport.pdf:application/pdf","DocDescription":"","CreateDate":"05/18/2019","Userid":null,"LastChnageDate":null,"LastChnageUserid":null,"DocTypeCode":"27","AttachmentFilename":"SyamOld_Passport","ExpirtyDate":null,"IsDeleted":null,"DocTypeCodeDescription":"Expense                                           ","ContentType":"application/pdf"}]

AJAX 调用:

$("#btnVendorSearch").click(function() {
if (RequiredFieldValidate() != false) {
    var SupplierNumber = $('#txtVendorNumber').val();
    $.ajax({
        type: "POST",
        url: getExactPath('/Supplier/GetSupDocuments'),
        async: false,
        data: {
            SupplierNo: SupplierNumber
        },
        dataType: 'Json',
        success: function(jsonData) {    
            var jsonobj = JSON.parse(jsonData);
            if (jsonobj.error == "" || jsonobj.error == undefined) {
                FillGrid(jsonobj);
            } else {
                console.log(jsonobj.error);
            }
        },
        error: function() {
            alert("Unable to fetch the data");
        }});}});

控制器方法:

public JsonResult GetSupDocuments(string SupplierNo) {
    string Error = "";
    DocumentumUtil dUtil = new DocumentumUtil();
    List < Attachment > AttachmentDetails = new List < Attachment > ();
    try {
    AttachmentDetails = dUtil.GetSupplierAttachments(SupplierNo);
    return this.Json(AttachmentDetails);
    } catch (System.Exception ex) {
        Error = ex.Message;
    }
    return this.Json(new {error = Error});
}

它应该返回如下。请帮帮我。

[object,object,object,object]

【问题讨论】:

  • 错误信息是什么?
  • 错误:JSON 未定义
  • 我刚刚知道 jQuery.parseJSON() 正在工作。但我只想知道 JSON.parse() / jQuery.parseJSON() / JSON.stringify() 之间的区别。

标签: c# jquery ajax asp.net-mvc-4


【解决方案1】:

JSON 字符串现在正在配对到数组对象中。

jQuery.parseJSON()

【讨论】:

    猜你喜欢
    • 2017-08-17
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多