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");
提示:json
Date
類型@odata.bind
,值爲/目標實體名(當前查找字段值id)
,目標實體名使用複數形式建立單條記錄api
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)) }
提示:app
select
:顯示記錄中哪些字段expand
:指定此記錄中查找類型
的字段,找出此字段對應的記錄orderby
:排序filter
:篩選&
符號鏈接查詢所有post
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{} } } }
根據主鍵查詢this
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{} } } }
根據主鍵查詢單個字段值url
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{} } } }
根據主鍵查詢外鍵字段值code
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{} } } }
條件查詢xml
算術:eq
,ne
,gt
,ge
,lt
,le
排序
$filter=money lt 100
邏輯:and
,or
,not
ip
$filter=money lt 100 and money lt 100 $filter=not money lt 100
模糊查詢:contains
,endswith
,startswith
$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)
提示:
JSON.stringify(data)