Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

java - spring mvc ajax json malformed sometimes

libraries used

  1. spring.4.3.4 jar
  2. jquery 1.9.1
  3. gson-2.2.2 jar

Using JDBCTemplate

Controller

@RequestMapping(value = "user/getApmcBudgTransData", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public @ResponseBody String getUserLevelMasterDetails(HttpServletRequest request,HttpServletResponse res) { 
        ModelAndView model = new ModelAndView();
        String yrmstid=request.getParameter("finyrMstid");
        String apmcid=request.getParameter("apmcid");
        String sendToOfc = request.getParameter("sendToOfc");   
        String opFlag = request.getParameter("opFlag"); 
        String formType = request.getParameter("formType"); 
        ArrayList<ApmcBudgTransaction> apmcBudgtrandata=budgetService.getApmcBudgTransData(apmcid, yrmstid, sendToOfc, opFlag, formType);       

        Gson gson = new Gson();
        return gson.toJson(apmcBudgtrandata);
    }

JSP ajax call

                  $.ajax({
                    url:"${pageContext.request.contextPath}/user/getApmcBudgTransData",
                    type:'post',  
                    dataType: 'json',
                    async:false,
                    data: {
                        finyrMstid :lastyr,
                        apmcid : pkvalue,
                        sendToOfc : "APMC",
                        opFlag : flag,
                        formType : "S"
                     },
                   success: function(resp) {},
                   error: function(err) {}

                   });

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-2.5.xsd">

    <context:component-scan base-package="com.me.you" />

    <context:annotation-config />

    <mvc:annotation-driven />
    <mvc:default-servlet-handler />


    <mvc:interceptors>


    <bean id="webContentInterceptor" 
          class="org.springframework.web.servlet.mvc.WebContentInterceptor">
        <property name="cacheSeconds" value="0"/>
        <property name="useExpiresHeader" value="true"/>
        <property name="useCacheControlHeader" value="true"/>
        <property name="useCacheControlNoStore" value="true"/>
    </bean>
    </mvc:interceptors>


    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>



</beans>    

The ajax call is receiving a large amout of utf-8 json data from controller.

The json is wellformed in the controller each and every time.

When the control is returned back to ajax call, the first time, the json is malformed and error function is called.

The second and each subsequent time though, the json is wellformed and success function is called.

Firebug screenshot

http://imgur.com/a/eHb5s

Malformed json

   [
  {
    "finYrID": "3",
    "budgetTransId": "18127210",
    "operationId": "",
    "commonTrno": "165",
    "suppliBudgYn": "N",
    "tranDate": "10/05/2016",
    "srNo": "1",
    "apmcGlMstId": "1",
    "apmcMstId": "165",
    "lastYrActualAmt": "2741406",
    "currYrActualAmt": "2562",
    "currYrExpAmt": "0",
    "currYrTotExpAmt": "2562",
    "apmcCurrBudgAmt": "2379",
    "disCurrBudgAmt": "2379",
    "divCurrBudgAmt": "2379",
    "apmcRemark": "",
    "distRemark": "",
    "divRemark": "",
    "msambRemark": "?????? ???",
    "sanctionRemark": "",
    "suppBudAmt": "0",
    "suppDistBudgAmount": "",
    "suppDivBudgAmount": "",
    "suppHoBudgetAmt": "",
    "suppBudgetRemark": "",
    "suppDisRemark": "",
    "suppDivRemark": "",
    "suppHoRemark": "",
    "suppSancRemark": "",
    "printPosition": "",
    "forwardStatus": "P",
    "lastYrBudgamt": "3035810",
    "msambCurYrBudgAmt": "2379",
    "glName": "????????? ??? ?????? ",
    "abrxFlg": "INCOME",
    "parentId": "",
    "allowEntry": "Y",
    "apmcTranDate": "12/05/2016",
    "distTranDate": "24/05/2016",
    "divTranDate": "06/06/2016",
    "hoTranDate": "19/07/2016",
    "sanctionDate": "20/09/2016",
    "docAppFlag": "",
    "suppBudgTrno": "100165",
    "apmcSuppTranDate": "",
    "distSuppTranDate": "",
    "divSuppTranDate": "",
    "hoSuppTranDate": "",
    "sancSuppDate": "",
    "suppDocAppFlag": "Y",
    "budgSendToOffice": "SANCTION",
    "suppSendToOffice": "APMC",
    "suppCurYrActualAmt": "2379",
    "suppCurYrExpectedAmt": "0",
    "suppCurYrTotExpectedAmt": "2379"
  },
  {
    "finYrID": "3",
    "budgetTransId": "18127209",
    "operationId": "",
    "commonTrno": "165",
    "suppliBudgYn": "N",
    "tranDate": "10/05/2016",
    "srNo": "181",
    "apmcGlMstId": "181",
    "apmcMstId": "165",
    "lastYrActualAmt": "0",
    "currYrActualAmt": "0",
    "currYrExpAmt": "304852",
    "currYrTotExpAmt": "304852",
    "apmcCurrBudgAmt": "438307",
    "disCurrBudgAmt": "1238307",
    "divCurrBudgAmt": "1148307",
    "apmcRemark": "",
    "distRemark": "898751.00",
    "divRemark": "",
    "msambRemark": "",
    "sanctionRemark": "",
    "suppBudAmt": "315514",
    "suppDistBudgAmount": "0",
    "suppDivBudgAmount": "0",
    "suppHoBudgetAmt": "0",
    "suppBudgetRemark": "",
    "suppDisRemark": "",
    "suppDivRemark": "",
    "suppHoRemark": "",
    "suppSancRemark": "",
    "printPosition": "",
    "forwardStatus": "P",
    "lastYrBudgamt": "0",
    "msambCurYrBudgAmt": "898751",
    "glName": "?????? ??? ?????? ",
    "abrxFlg": "EXPENSES",
    "parentId": "",
    "allowEntry": "Y",
    "apmcTranDate": "12/05/2016",
    "distTranDate": "24/05/2016",
    "divTranDate": "06/06/2016",
    "hoTranDate": "19/07/2016",
    "sanctionDate": "20/09/2016",
    "docAppFlag": "",
    "suppBudgTrno": "100165",
    "apmcSuppTranDate": "",
    "distSuppTranDate": "",
    "divSuppTranDate": "",
    "hoSuppTranDate": "",
    "sancSuppDate": "",
    "suppDocAppFlag": "Y",
    "budgSendToOffice": "SANCTION",
    "suppSendToOffice": "APMC",
    "suppCurYrActualAmt": "957593",
    "suppCurYrExpectedAmt": "",
    "suppCurYrTotExpectedAmt": "957593"
  }
]

I cannot for the love of programming figure out why this is happening.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...