gpt4 book ai didi

javascript - 如何使用Ajax调用Java Controller 正确获取用户定义的对象?

转载 作者:行者123 更新时间:2023-12-01 16:14:41 25 4
gpt4 key购买 nike

如何使用Ajax调用Java控制器正确获取用户定义的对象?

下面是对象类:

@SuppressWarnings("serial")
@Entity
@Table(name = "is_sector")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)


public class Sector implements java.io.Serializable {

private BigDecimal id;
private String nameEn;
private String nameAr;
private Organization org;
private ApplicationUser manager;
private BigDecimal userGroupId; //USER_GROUP_ID
private ApplicationUser specialist;
private BigDecimal orderId;
private BigDecimal active;

/**
* Instantiates a new sector.
*/
public Sector() {
}

/**
* Instantiates a new sector.
*
* @param id the id
*/
public Sector(BigDecimal id) {
this.id = id;
}

/**
* Instantiates a new sector.
*
* @param id the id
* @param nameEn the name en
* @param nameAr the name ar
* @param orgId the org id
*/
public Sector(BigDecimal id, String nameEn, String nameAr,Organization org) {
super();
this.id = id;
this.nameEn = nameEn;
this.nameAr = nameAr;
this.org = org;

}


/**
* Instantiates a new sector.
*
* @param id the id
* @param nameEn the name en
* @param nameAr the name ar
* @param orgId the org id
* @param userGroupId the user group id
*/
public Sector(BigDecimal id, String nameEn, String nameAr,Organization org, BigDecimal userGroupId,ApplicationUser manager) {
super();
this.id = id;
this.nameEn = nameEn;
this.nameAr = nameAr;
this.org = org;
this.userGroupId = userGroupId;
this.manager = manager;
}



/**
* Gets the user group id.
*
* @return the user group id
*/
@Column(name = "user_group_id", nullable = true, precision = 22, scale = 0)
public BigDecimal getUserGroupId() {
return userGroupId;
}

/**
* Sets the user group id.
*
* @param userGroupId the new user group id
*/
public void setUserGroupId(BigDecimal userGroupId) {
this.userGroupId = userGroupId;
}

............................................
............................................


下面是控制器方法:

    @RequestMapping(value = "/getSectorById/{sectorId}")
public Sector getSectorById(ModelAndView mav, @PathVariable BigDecimal sectorId) {

return getSector(sectorId);
}


public Sector getSector(BigDecimal sectorId) {

Sector sector=null;
try {
Sector exampleSector = new Sector();
exampleSector.setId(sectorId);
sector=lookupService.findSector(exampleSector) ;

}
catch (Exception e) {
e.printStackTrace();
}

return sector;
}


当我尝试以这种方式访问​​它时,它没有进入成功方法,并且响应错误我看到“ [object Object]”。

function getSectorById(sectorId){

var sector=null;
$.ajax({
type: "POST",
async: false,
url: contextPath + "/secure/services/getSectorById/"+sectorId,
contentType: contentTypeJSON,
success: function(data) {
alert(data);
sector= JSON.parse(data);

},
error: function(response){
alert(response); //THIS SHOWS "[object Object]"
}

});

return sector;
}


怎么做对?

最佳答案

您使用JSON.stringify()@RestController

function getSectorById(sectorId){

var sector=null;
$.ajax({
type: "POST",
async: false,
url: contextPath + "/secure/services/getSectorById/"+sectorId,
contentType: contentTypeJSON,
success: function(data) {
alert(data);
secter = JSON.stringify(data);

},
error: function(response){
alert(response);
}

});

return sector;
}

关于javascript - 如何使用Ajax调用Java Controller 正确获取用户定义的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62437964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com