【问题标题】:Ajax call - Response is always nullAjax 调用 - 响应始终为空
【发布时间】:2018-12-18 06:24:05
【问题描述】:

所以,我正在使用 Backbone 和 Spring。我想从输入字段中发布数据,问题是我可以在 chrome 调试中看到数据,数据在那里它不为空,但是当我查看我的数据库时,该字段为空,并且当我在其中放置断点时eclipse 请求所在的位置,为空。

Backbone,这里只看 ajax 代码,但我想它工作正常,否则我不会在 chrome 调试中看到数据。

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();
},

这是我的 spring 控制器代码,我已经搜索了整个谷歌,我使用了其他代码我使用了所有的东西,没有任何工作我总是得到 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}";
}

我认为 HTML 不是那么重要,因为您可以看到我的输入字段具有 ID #newConfigName 等,并且这些值被移交给变量,因为我在 Chrome 中对其进行了调试。

【问题讨论】:

    标签: ajax spring hibernate model-view-controller


    【解决方案1】:

    contentType: "application/json; charset=UTF-8" 表示要传递参数为JSON,因此在服务器端,需要解析JSON 对象。

    如果你想通过request.getParameter()获取参数,你需要删除这行。

    $.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);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2011-09-17
      • 2016-03-27
      • 1970-01-01
      • 1970-01-01
      • 2020-07-23
      • 1970-01-01
      • 2014-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多