Dynamics 365(一)JS调用

JSAPI请求头

xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xmlhttp.setRequestHeader("Accept", "application/json");
xmlhttp.setRequestHeader("OData-MaxVersion", "4.0");
xmlhttp.setRequestHeader("OData-Version", "4.0");

// 加上成功返回201,省略成功返回204
xmlhttp.setRequestHeader("Prefer", "return-representation");

创建操作

提示:

  • 日期类型:值为Date类型
  • 查找类型:字段后追加@odata.bind,值为/目标实体名(当前查找字段值id),目标实体名使用复数形式

创建单条记录

function createEntity(){
    var entity = {}
    entity['new_name'] = 'dtcc'	//文本
    entity['new_gender'] = 1000	//选项集
    entity['new_time'] = new Date()	//日期
    entity['new_search@odata.bind'] = '/new_tbs(id)'	//查找
    entity['new_bool'] = true
    
    var req = new XMLHttpRequest()
    req.open('post','url/accounts',false)
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.onreadystatechange = function(){
        if(this.readyState == 4){
            if(this.status == 204){}
            else{}
        }
    }
    req.send(JSON.stringify(entity))
}

查询操作

提示:

  • select:显示记录中哪些字段
  • expand:指定此记录中查找类型的字段,找出此字段对应的记录
  • orderby:排序
  • filter:筛选
  • 多个关键字&符号连接

查询全部

function getNameByPrimaryKey(){
    var req = new XMLHttpRequest()
    req.open('get','http://url/accounts',false)
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.onreadystatechange = function(){
        if(this.readyState == 4){
            if(this.status == 204){
                var result = JSON.parse(this.responseText)
            }
            else{}
        }
    }
}

根据主键查询

function getDataByPrimaryKey(){
    var req = new XMLHttpRequest()
    req.open('get','http://url/accounts(id)?$select=name,accountid&$expand=territoryid($select=description)',false)
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.onreadystatechange = function(){
        if(this.readyState == 4){
            if(this.status == 204){
                var result = JSON.parse(this.responseText)
            }
            else{}
        }
    }
}

根据主键查询单个字段值

function getNameByPrimaryKey(){
    var req = new XMLHttpRequest()
    req.open('get','http://url/accounts(id)/name',false)
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.onreadystatechange = function(){
        if(this.readyState == 4){
            if(this.status == 204){
                var result = JSON.parse(this.responseText)
            }
            else{}
        }
    }
}

根据主键查询外键字段值

  • territoryid为查找类型,可以理解为一个实体记录
function getNameByPrimaryKey(){
    var req = new XMLHttpRequest()
    req.open('get','http://url/accounts(id)/territoryid($select=description)',false)
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.onreadystatechange = function(){
        if(this.readyState == 4){
            if(this.status == 204){
                var result = JSON.parse(this.responseText)
            }
            else{}
        }
    }
}

条件查询

  • 算术:eqnegtgeltle

    $filter=money lt 100
    
  • 逻辑:andornot

    $filter=money lt 100 and money lt 100
    $filter=not money lt 100
    
  • 模糊查询:containsendswithstartswith

    $filter=contains(name,'nc')
function getNameByKey(){
    var filter = '?$filter=contains(name,'nc')'
    var req = new XMLHttpRequest()
    req.open('get','http://url/accounts/'+filter,false)
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.onreadystatechange = function(){
        if(this.readyState == 4){
            if(this.status == 204){
                var result = JSON.parse(this.responseText)
            }
            else{}
        }
    }
}

排序

'http://url/users?$select=name,revenue&$orderby=name asc,revenue desc&$filter=name eq 3'

TOP

'http://url/users?top=3'

COUNT

'http://url/users?count=true'

更新操作

更新单条记录

function createEntity(){
    var entity = {}
    entity['new_name'] = 'dtcc'	//文本
    entity['new_gender'] = 1000	//选项集
    entity['new_time'] = new Date()	//日期
    entity['new_search@odata.bind'] = '/new_tbs(id)'	//查找
    entity['new_bool'] = true
    
    var req = new XMLHttpRequest()
    req.open('PATCH','url/accounts(Id)',false)
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    
    // 记录不存在则创建
    req.setRequestHeader("If-Match", "*");
    
    req.onreadystatechange = function(){
        if(this.readyState == 4){
            if(this.status == 204){}
            else{}
        }
    }
    req.send(JSON.stringify(entity))
}

更新记录的单个属性值

var entity = '{value:33}'
req.open('PUT','url/accounts(Id)/name',false)
req.send(JSON.stringify(entity))

删除操作

删除单条记录

req.open('delete','url/accounts(Id)',false)

清空记录属性值

req.open('delete','url/accounts(Id)/name',false)

HTTP.js

提示:

  • 参数必须JSON格式化:JSON.stringify(data)

转载来源:https://www.cnblogs.com/weiyongguang/p/14060776.html

posted @ 2022-04-06 11:25  wulala9  阅读(230)  评论(0编辑  收藏  举报