通过Javascript Remoting在Visualforce页面上显示JSON内容 - 意外的令牌错误

时间:2018-04-27 22:08:33

标签: javascript json visualforce apex

我正在尝试创建一个Visualforce页面,在单击按钮时显示自定义对象信息(字段),如下所示:

enter image description here

按钮单击激活我的Apex类中的方法(它充当Visualforce页面的控制器扩展)。该方法查询自定义对象并返回包含JSON格式信息的String。

然后,在Visualforce页面中,我使用Javascript将该方法的结果打印到带有 JSON.parse(结果)的页面,但我收到 “意外的令牌和放大器;在位置1的JSON中“

如果我尝试在没有JSON.parse()的情况下打印结果,它将打印字符串,但它仍然是JSON格式(即:带花括号,引号,冒号): enter image description here

Visualforce页面

<apex:page title="VF Page" standardController="case" extensions="ControllerExtension">

<apex:includeScript value="{!$Resource.JQueryJs}"/>


<script type="text/javascript">
    function getRemoteInfoJSON() {
        var var1 = "{!case.Var1__c}";
        var var2 = "{!case.Var2__c}";

        Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.ControllerExtension.getInfoJSON}', var1, var2, 
            function(result, event){
                if(event.status){
                    try {
                        document.getElementById("pushDataHere").innerHTML = JSON.parse(result); //produces error; if we just use 'result' instead of 'JSON.parse(result)', the string is printed in JSON format, with curly braces and such.
                    }catch(err) {
                        document.getElementById("pushDataHere").innerHTML = err.message;
                    }
                }
            }
        );
    };
</script>

<button onclick="getRemoteInfoJSON()">BUTTON</button> <br /> <br />
<div id="pushDataHere">
</div>

</apex:page>

来自Apex Class的方法(扩展名):

@RemoteAction
global static String getInfoJSON(String var1, String var2){
    sObject record = [SELECT <lots of fields> FROM Custom_Object__c WHERE Platform__c = :var1 AND ParentProductId__c = :var2];
    Map<String, Schema.SObjectField> fieldMap = Schema.getGlobalDescribe().get('Custom_Object__c').getDescribe().fields.getMap();

    Map<String, String> contents = new Map<String, String>();
    for(String fieldName : fieldMap.keySet()){
        try{
            String fieldLabel = fieldMap.get(fieldName).getDescribe().getLabel();
            String fieldContents = String.valueOf(record.get(fieldName));
            //We only want to add to our Contents list if the field has something in it
            if (fieldContents != null){
                contents.put(fieldLabel, fieldContents);
            }
        } catch (SObjectException e){
            System.debug('Whoopsie'); //Occurs when we try to retrieve contents of a field we didn't query in the 'record' variable.
        }
    }
    return JSON.serialize(contents); //returns '{"Field1": "Value1","Field2": "Value2", "Field3": "Value3", "Field4": "Value4", "Field5": "Value5", "Field6": "Value6", "Field7": "Value7", "Field8": "Value8", "Field9": "Value9", "Field10": "Value10","Field11": "Value11","Field12": "Value12","Field13": "Value13","Field14": "Value14"}'
}

Apex Class返回的字符串:

{
    "Field1": "Value1",
    "Field2": "Value2",
    "Field3": "Value3",
    "Field4": "Value4",
    "Field5": "Value5",
    "Field6": "Value6",
    "Field7": "Value7",
    "Field8": "Value8",
    "Field9": "Value9",
    "Field10": "Value10",
    "Field11": "Value11",
    "Field12": "Value12",
    "Field13": "Value13",
    "Field14": "Value14"
}

按钮点击时收到错误:

Unexpected token & in JSON at position 1

enter image description here

非常感谢任何见解。

0 个答案:

没有答案
相关问题