【问题标题】:Spring MVC-JQuery-AJAX Updating Single row from table using Ajax and Spring MVCSpring MVC-JQuery-AJAX 使用 Ajax 和 Spring MVC 从表中更新单行
【发布时间】:2017-08-23 11:20:30
【问题描述】:

下面的代码给了我 AJAX CALL 的 400-BAD REQUEST 错误。

在下面的代码中有一个表格,我在其中填充所有学生记录,例如 带有编辑和删除按钮的卷号、名字、姓氏和地址 现在我正在处理 EDIT 按钮。

要求:- 单击编辑按钮,它应该使表属性可用于编辑该特定行,编辑后单击更新,它应该更新到数据库中,并且应该再次显示表中的不可编辑字段。 下面是我的控制器类

@RequestMapping(value="/updatetemp",method = RequestMethod.POST,produces = 
{"application/json"})  
SQLException{
@ResponseBody
  public boolean edit(@RequestParam("rollnumber") int rollnumber,
                           @RequestParam("firstname") String firstname,
                           @RequestParam("surname") String surname,
                           @RequestParam("address") String address)
                                                    throws SQLException{
        Student s = new Student();
        s.setRollnumber(rollnumber);
        s.setFirstname(firstname);
        s.setSurname(surname);
        s.setAddress(address);
        if(this.studentService.update(s)){
            return true;
        }else{
            return false;
        }
    }

下面是我的 Jquery 和 Ajax 调用代码

$(document).ready(function() {
$(document).on('click','#editlink',function(){
    var par = $(this).parent().parent();
    var tdName = par.children("td:nth-child(2)"); 
    var tdPhone = par.children("td:nth-child(3)"); 
    var tdEmail = par.children("td:nth-child(4)");
    tdName.html("<input type='text' class='firstname' 
value='"+tdName.html()+"'/>");
    tdPhone.html("<input type='text' class='lastname' 
value='"+tdPhone.html()+"'/>");
    tdEmail.html("<input type='text' class='address' 
value='"+tdEmail.html()+"'/>");
    $("#editlink").prop('disabled',true);
});

$(document).on('click','#updatelink',function(){
    var rollnumber = $(this).parent().siblings('.rollnumber').text();
    var firstname;
    var surname;
    var address;
$(this).parent().siblings("td.firstname").
find("input.firstname").each(function() {
        firstname = (this.value);
    });
    $(this).parent().siblings("td.lastname").
find("input.lastname").each(function() {
        surname = (this.value);
    });
    $(this).parent().siblings("td.address").
find("input.address").each(function() {
        address = (this.value);
    });

    var studentjson = { rollnumber: rollnumber, firstname: firstname, 
surname: surname,address: address };

    $.ajax({
        type: 'POST',
        url: 'http://localhost:8080/SpringDemo/updatetemp',
        data: studentjson,
        contentType: 'application/json; charset=utf-8',
        success: function (response) { 
            console.loge(response);
            if(response == true){
                $(this).parent().siblings("td.firstname").html("<td 
 class='firstname'>"+firstname+"</td>");
                $(this).parent().siblings("td.lastname").html("<td 
class='lastname'>"+surname+"</td>");
                $(this).parent().siblings("td.address").html("<td 
class='address'>"+address+"</td>");
            }
        }
    });

});

我认为主要问题在于我的 AJAX 调用....请帮助我。这两天我在努力……我是新手!

【问题讨论】:

  • 你试过没有设置contentType选项吗?
  • 你说的是方法produces = {"application/json"},但是你返回的是一个布尔值......
  • 我真的不知道该怎么办??我应该删除 contentType 并生成吗?然后试试?
  • 一次尝试一个,看看是否有任何变化。
  • 我从 ajax 调用中删除了内容类型,现在它给出了 500 内部错误

标签: jquery ajax spring-mvc


【解决方案1】:

您的控制器有 @RequestParam 而您在调用中将 studentjson 作为正文传递。将某些内容作为请求参数传递,然后将数据发送为

"'http://localhost:8080/SpringDemo/updatetemp"+ "?rollnumber=" + rollnumber + "&firstname=" + firstname + "&surname=" + surname  + "&address=" + address

相反,我会要求您将控制器的编辑方法更改为 @RequestBody POJOClass pojoClass 并且所有字段 rollnumber、firstname、surname、address 应该是 POJOClass 中的属性

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多