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

标签 javascript java ajax spring model-view-controller

如何使用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/

相关文章:

javascript - 期望从 promise 分配的值(Jest unittest)

java - 如何将从两个表中检索的数据存储到 java/servlet/jsp 中的另一个表中

jquery - 雅虎 YQL RSS - 错误请求

php - 使用 Javascript 和 PHP 动态添加查询 XML 源的新表和行

javascript - 修复 html 表格的表头在顶部

javascript - jsPlumb - 刷新后端点的错误位置和鼠标悬停后它们到达正确的位置

javascript - 如何使用可变参数过滤javascript对象数组

java - Jackson hibernate 模块 - 渴望特定请求

java - TCP 打洞 - 如何进行此步骤

php - PHP 跨域的 Ajax GET 响应