最近用到的几种前后端交互方式
生活随笔
收集整理的這篇文章主要介紹了
最近用到的几种前后端交互方式
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?????? 后端用的是Mybatis框架,前端的話由于是基于Ionic框架開(kāi)發(fā),所以大多都是通過(guò)Ionic的組件進(jìn)行前后端交互 ,另外還涉及到一些地圖功能用到ArcGIS JS API ,必然就會(huì)用到dojo的一些東西,尤其是向ArcGIS Server請(qǐng)求服務(wù)時(shí)。
1.dojo
1.1 "dojo/_base/xhr"
XmlHttpRequest,這個(gè)就是類(lèi)似于ajax的東西,其實(shí)對(duì)這個(gè)沒(méi)啥理解,上代碼。
var content = {"layerAlias": 'road'}; var contextPath = "http://localhost:8080/test" xhr.post({url : contextPath+"/getData/showfields",//后臺(tái)請(qǐng)求地址handleAs : 'json',content:content,//參數(shù)load : lang.hitch(this, function(response){this.function123(response);//回調(diào)函數(shù)}),error : function(response, ioArgs) {}});1.2 "dojo/request"
request(this.contextPath + 'getDatas/getFields',{handleAs:'json', }).then(function(response){console.log(response); });2.Ionic
Ionic向后臺(tái)請(qǐng)求的話,先從當(dāng)前Page頁(yè)面調(diào)用Provider里面的Service(要聲明為NgModule才可以在Page中引用),然后通過(guò)service向后臺(tái)請(qǐng)求。
?
Page頁(yè)面的請(qǐng)求:
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("錯(cuò)誤", error);})}Provider向后端發(fā)送請(qǐng)求:
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();//轉(zhuǎn)換為json數(shù)據(jù)if (!!result) {return result;}}).catch(this.conf.handleErrorService);}?
總結(jié)
以上是生活随笔為你收集整理的最近用到的几种前后端交互方式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Mybatis的xml文件中$ 与 #的
- 下一篇: Mybatis返回值类型为Map时,空值