0

我在java中有一个自定义类,类似于

    public class AddressesVO {

private Long addressId;
private String address;

public Long getAddressId() {
    return addressId;
}
public void setAddressId(Long addressId) {
    this.addressId = addressId;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}

}

我的弹簧功能看起来像这样,

     @RequestMapping(value="/searchAddress", method=RequestMethod.POST)
    @ResponseBody
    public String searchAddress(HttpServletRequest request, HttpServletResponse response, @RequestBody        AddressesVO abc) {
System.out.println("This is the AddressesVO  Object" +abc);
    }

我正在对上述函数进行 ajax 调用并将 javascript 对象作为数据传递。

$.ajax({
type: "POST",  
url: "searchAddress/",
data: objname, 
success: function(){  
   alert('Form submitted');
            alert(response);
},
error: function(XMLHttpRequest, textStatus, errorThrown) { 
    alert('failure'+response); 
}       

});

但我收到一条错误消息,提示客户端发送的请求在语法上不正确,并且它的运行失败

任何人都可以帮助我如何将 JS 对象发送到 spring 并将其作为自定义类接受。

4

1 回答 1

0

创建一个 javascript 对象 var AddressesVO = new Object();

填充其字段

AddressesVO.addressId = 111;
AddressesVO.address = 'some address';

将此对象传递给 ajax 调用(注意:数据:AddressesVO)

$.ajax({
    type: "POST",  
    url: "searchAddress/",
    data: AddressesVO, 
    success: function(){  
        alert('Form submitted');
        alert(response);
    },
error: function(XMLHttpRequest, textStatus, errorThrown) { 
    alert('failure'+response); 
}

改变spring方法(注:@ModelAttribute AddressesVO)

@RequestMapping(value="/searchAddress", method=RequestMethod.POST)
@ResponseBody public String searchAddress(HttpServletRequest request, HttpServletResponse response, @ModelAttribute AddressesVO abc) {
    System.out.println("This is the AddressesVO  Object" +abc);
}

希望这对你有用。

于 2015-07-31T09:36:38.023 回答