最近用到的几种前后端交互方式
生活随笔
收集整理的這篇文章主要介紹了
最近用到的几种前后端交互方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?????? 后端用的是Mybatis框架,前端的話由于是基于Ionic框架開發,所以大多都是通過Ionic的組件進行前后端交互 ,另外還涉及到一些地圖功能用到ArcGIS JS API ,必然就會用到dojo的一些東西,尤其是向ArcGIS Server請求服務時。
1.dojo
1.1 "dojo/_base/xhr"
XmlHttpRequest,這個就是類似于ajax的東西,其實對這個沒啥理解,上代碼。
var content = {"layerAlias": 'road'}; var contextPath = "http://localhost:8080/test" xhr.post({url : contextPath+"/getData/showfields",//后臺請求地址handleAs : 'json',content:content,//參數load : lang.hitch(this, function(response){this.function123(response);//回調函數}),error : function(response, ioArgs) {}});1.2 "dojo/request"
request(this.contextPath + 'getDatas/getFields',{handleAs:'json', }).then(function(response){console.log(response); });2.Ionic
Ionic向后臺請求的話,先從當前Page頁面調用Provider里面的Service(要聲明為NgModule才可以在Page中引用),然后通過service向后臺請求。
?
Page頁面的請求:
getFieldsCol() {this.fieldQueryService.getFields(this.layerAlias).subscribe(pageResult => {this.fieldsColumn = [];for(let item of pageResult){let obj = {name:'' , prop:''};obj.name = item.aliasName;obj.prop = item.fieldName;this.fieldsColumn.push(obj);}},error => {this.commonConfig.handleErrorView("錯誤", error);})}Provider向后端發送請求:
getFields(keyWord: string): Observable<Array<FieldName>>{let method = "/getFields";let params: URLSearchParams = new URLSearchParams();let options = { headers: this.headers };params.set("layerAlias", keyWord)let url = this.baseUrl + method;this.headers = this.conf.getDefaultAjaxHeaders();options = Object.assign(options, { search: params });return this.http.get(url, options).map(res => {let result = res.json();//轉換為json數據if (!!result) {return result;}}).catch(this.conf.handleErrorService);}?
總結
以上是生活随笔為你收集整理的最近用到的几种前后端交互方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mybatis的xml文件中$ 与 #的
- 下一篇: Mybatis返回值类型为Map时,空值