سؤال

I have a view form that contains a table with textfield inside it. I need to save the information of the table inside a database.

* javascript function *

function sendTableToServer()
{
    var table; 
    table = document.getElementById('myTable');
    var rowLength = table.rows.length;
    var tableArray = [rowLength -1];

    var JSONObject;

    for (var tblRow = 1; tblRow < rowLength; tblRow++){
            console.log("***** ROW CHANGE *****");

            JSONObject = {
                "rowIndex": "myTableRow" + tblRow,
                "partNo": table.rows[tblRow].cells[0].firstChild.value,
                "partName":table.rows[tblRow].cells[1].firstChild.value,                     
            };
        f24Array[tblRow] = JSONObject;
    }
    console.log(f24Array[rowLength-1]["rowIndex"]);
    console.log(f24Array.length -1);

        $.getJSON("f24BatcCreateEAF.do", {
            type: "F24", 
            pRefForm: "DVL",  
            pF24Values: tableArray 
            } 
        , function(ans)  {
            console.log("The row is" +ans.strVar);
        }
    );
};

* controller *

public @ResponseBody
AjaxReturnMessage createEaf( Model model, HttpServletRequest pRequest, @RequestParam String type,  @RequestBody String[] pF24Values ) throws Exception {

    long eafId=0;
    AjaxReturnMessage pARM = new AjaxReturnMessage();
    log.info( "entering creatEaf );

        try {

            System.out.println("calling gF24BatchService");

            eafId = gF24BatchService.createEAFRow( model, pp, type, pRefForm, pDescription );


            pARM.setId( eafId );
            pARM.setStrVar( "f24TableRow1" );
            pARM.setCode( AjaxReturnMessage.STATUS_ERROR );
        } catch ( Exception e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // return the jsp page only
        return pARM;

}

When i trigger the sendTableToServer function i get an error "404". But if i remove the pF24Values from the JSON call and he service then there is no error.

How would it be possible for me to "catch" pF24Values without having to create a new object type. is this possible?

Thanks

هل كانت مفيدة؟

المحلول

You are performing a GET to your controller, so that controller can only capture data via request parameters. In addition, your getJSON function is sending all the data as request parameters as well.

Therefore your controller has to capture pF24Values as a @RequestParam, not as a @RequestBody.

If you want to send complex data structures to your controller you need to do a POST with the JSON as the request body (for instance) - although you can use GET for array data but not the way you are doing it here - each row needs to have a separate request param(e.g. pF24Values:one and pF24Values:two, etc - there are a few ways to do this).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top