jakubB
jakubB

Reputation: 35

Ajax call - Response is always null

So, I am using Backbone and Spring. I want to POST data from an Input field and the problem is I can see the data in chrome debug, the data is there it is not null but when I look then in my database the field is null and also when I put a breakpoint in eclipse where the request is, it is null.

Backbone, here just watch the ajax code, but it Works fine I guess otherwise I wouldn't see the data in chrome debug.

saveNewName : function(e) {
    var inputValue = $('#newConfigName').val();
    var settingId = $('#selectReportChannel').val();
    var data = {
            "inputValue" : inputValue,
            "settingId" : settingId,

    };

    $.ajax({
        type: "POST",
        url: "../customchannel",
        async: false,
        contentType: "application/json; charset=UTF-8",
        dataType: "json",
        data: JSON.stringify(data),
        success: function (data){
            $('.alertCustomChannel').show();
            setTimeout(function() {
                $("#alertID").hide();
            }, 2000);
        },
        error: function(){
            console.log('Failure in saving new Config Name!')
            $('.alertCustomChannel2').show();
            setTimeout(function() {
                $("#alertID2").hide();
            }, 2000);
        }
    });
    e.stopImmediatePropagation();
},

Here is my spring controller code, I've searched the whole google, I used other codes I used everything nothing works I always get null.

@RequestMapping(value = "/customchannel", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody    
String saveNewConfigName(@RequestBody String newConfigName,HttpServletRequest request) {
    String configName = request.getParameter("inputValue");     
    System.out.println(request.getParameter("inputValue"));

    System.out.println(request.getSession().getAttribute("inputValue"));
    customchannelservice.saveNewConfigName(configName);
    Enumeration paramaterNames = request.getParameterNames();
    while(paramaterNames.hasMoreElements() ) {
           System.out.println(paramaterNames.nextElement());
    } 
    return "{\"success\": true}";
}

I think HTML is not so important, as you can see my Input fields have the IDs #newConfigName etc. and the values are handed over to the variable because I debugged it in Chrome.

Upvotes: 0

Views: 251

Answers (1)

flyingfox
flyingfox

Reputation: 13506

contentType: "application/json; charset=UTF-8" means you want to pass the parameter as JSON,thus in the server side,you need to parse JSON object.

If you want to get parameter via request.getParameter(),you need to remove this line.

$.ajax({
    type: "POST",
    url: "../customchannel",
    async: false,
    //contentType: "application/json; charset=UTF-8",//remove this line
    dataType: "json",
    data: data,//send the data parameter directly
    success: function (data){
        $('.alertCustomChannel').show();
        setTimeout(function() {
            $("#alertID").hide();
        }, 2000);
    },
    error: function(){
        console.log('Failure in saving new Config Name!')
        $('.alertCustomChannel2').show();
        setTimeout(function() {
            $("#alertID2").hide();
        }, 2000);
    }
});

Upvotes: 1

Related Questions