課時一:JS操做數據

課時一: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");

建立操做

提示: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

  • 算術:eqnegtgeltle排序

    $filter=money lt 100
  • 邏輯:andornotip

    $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)
相關文章
相關標籤/搜索