wendux / fly Goto Github PK
View Code? Open in Web Editor NEW:rocket: Supporting request forwarding and Promise based HTTP client for all JavaScript runtimes.
Home Page: https://wendux.github.io/dist/#/language
License: MIT License
:rocket: Supporting request forwarding and Promise based HTTP client for all JavaScript runtimes.
Home Page: https://wendux.github.io/dist/#/language
License: MIT License
interceptors 拦截,如果 response.data 为 ‘’,即空值 , return response.data返回了整个response, 而不是一个空值
为什么我拿不到错误的body信息,这样后台返回的错误内容,我这边没法显示出来啊?
就像axios这样,在创建实例的时候指定参数的序列化方式。paramsSerializer, transformRequest
const $solr = axios.create({
baseURL: '/baseurl/solr/',
data: { indent: "off", wt: 'json', q: '*:*' },
paramsSerializer: (params) => Qs.stringify(params, { arrayFormat: 'repeat', encode: true }),
transformRequest: (data) => Qs.stringify(data, { arrayFormat: 'repeat', encode: true }),
})
没有找到相关文档
如下参考 fetch 的配置方法似乎也没用 ?
headers: { 'Content-Type': 'application/json; charset=utf-8'}
原因是:
Content-Type:"application/json;charset=utf-8"
网上都说要改成
Content-Type:"application/x-www-form-urlencoded"
400 错误是客户端错误,应该抛出错误,但是 response 还是应该反序列化为 json (如果 content-type 是 json 的话) 还是有必要的,因为需要传递错误信息,甚至连 500 错误都应该尝试这样做
由于小程序的wx.request使用不了patch方法,简单封装wx.request的话,并不能使用patch方法,看到Fly文档说有说到可以用使用patch方法。怎么才能让它支持使用patch方法呢?是需求我自己更换别的http引擎吗?求解。
使用场景:小程序
一、请求拦截器在多个并发请求时无效;
二、多个并发请进时,回调好像不能同时获取到;
this.$http
.get(
"https://api.douban.com/v2/movie/in_theaters?apikey=0b2bdeda43b5688921839c8ecb20399b&city=%E5%8C%97%E4%BA%AC&start=0&count=100&client=&udid=",
{
headers: {
"Content-Type": "json",
token: "123"
}
}
)
.then(d => {
//输出请求数据
console.log(d.data);
//输出响应头
console.log(d.header);
})
.catch(err => {
console.log(err.status, err.message);
});
在dva中使用fly.js ,response header是个空对象
后来看了下chrome的控制台,发现Content-Type还是application/json;charset=UTF-8,下面是我设置的代码,无法设置成功
`
import flyio from "flyio";
flyio.config.baseURL = URL_API_BASE_DOMAIN
flyio.config.timeout = 10000
flyio.config.withCredentials = true
flyio.config.headers = {
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
}
`
为啥没有判断304的情况,这时候直接走到reject了啊......
某个接口的返回值为以下格式,且返回的 Content-Type 为 application/json
var tag_jsonp =[{
"newsid":"351341",
"img":"http://img.ithome.com/newsuploadfiles/thumbnail/2018/3/351341_240.jpg",
"newstitle":"乐视网复牌跌停,封单逾70万手",
"postdate":"2018.03.16",
"url":"https://www.ithome.com/html/it/351341.htm"
}]
目前的判断方式依然会自动进行 JSON 解析,导致出错
if ((engine.getResponseHeader("Content-Type") || "").indexOf("json") !== -1
&& !utils.isObject(response)) {
response = JSON.parse(response);
}
后端是第三方的,所以无法控制
目前正在用一款支持低版本IE的mvvm框架(yoxjs),ajax还在用jquery的。
如果配合上fly,就可以安心删掉jquery了...
从网上找到的解决方案是:在设置responseType之前执行一次engine.open()。
这里是不是可以把responseType的设置挪到engine.open()后面。
let stream = weex.requireModule('stream');
var modal = weex.requireModule('modal')
var Fly = require("flyio/dist/npm/weex")
var fly=new Fly
export default {
methods:{
/*是否iphoneX*/
isIpx() {
return weex && (weex.config.env.deviceModel === 'iPhone10,3' || weex.config.env.deviceModel === 'iPhone10,6');
},
flyGET(api, callback){
modal.toast({message: '加载fly'})
return fly.get(api).then(callback)
},
get 没有反应, 怎么回事,
由于我们自己的源代码被 transform-runtime 处理过,所以 Promise 的类型会变成 object. 但是 fly 内部的 Promise 依然是原生的 Promise, 所以判断会出问题
我们使用的小程序 / 用了美团的 mpvue.
希望能有cordova插件支持Android和IOS,如果可以也支持uwp吧,在这方面,这项目用处很大!
我在使用拦截器的时候,发现报错信息只有一个status 是可用的 我在测试中遇到过 只返回过status 0 的问题,想询问下 ,这一块的处理如何自定义呢?我想能够得到服务器返回的整个body的内容?
我想在拦截器中捕捉到错误,然后拦截器中统一给错误提示信息,这点拦截器不支持?
能否有weex中调用的文档
鉴于 Restful API 的广泛使用,仅仅 GET 和 POST 似乎还不太够,Fly 用起来很爽,如果能加上对 PUT 和 DELETE 等 HTTP 方法的支持就更好了。
PS:本以为可以通过request()
方法手动发起,结果看到非 GET 请求只有 engine.open('POST')
,只好来提 Issue 了
我打算用fly做一个上传文件的功能,需要显示上传进度,axios可以通过配置onUploadProgress获得上传进度,不知道fly有没有相应的实现?
补充一下,我说的是浏览器环境
Response Headers:
x-powered-by:"Iris Web Framework"
vary:"Origin"
x-access-token:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySW5mbyI6eyJpZCI6MjEsInVzZXJuYW1lIjoi5p2O5r2H5r2HIiwicGhvbmUiOjE3NjEyODg3ODY4fSwiaWF0IjoxNTI0NTQyNjY4LCJleHAiOjE1MjQ1NDk4Njh9.Q1koqxGS9Aa6auS8dzuXTf21LvA4kaFd1woSlEjQpxI"
content-type:"application/json; charset=utf-8"
x-response-time:"19ms"
content-length:"1153"
date:"Tue, 24 Apr 2018 04:04:28 GMT"
connection:"keep-alive"
headers:
content-type :"application/json; charset=utf-8"
谢谢。。
因为没有使用独立的业务错误码,而是将错误归类到了http error里,并将一些错误信息写入body返回。因此需要在请求错误的信息里拿到body。谢谢!
在request拦截器中,可以弹出loading提高用户体验,这很棒。
但我在实际项目中,有的请求需要弹出loading,有的请求不需要,可以在发起请求的时候传一些参数进拦截器,拦截器再根据这个参数判断是否要显示loding这样吗?比如说:
fly.get(url,params,{isShowLoading:false});
不知有没有类似的解决方法?
升级到0.4.7后 post请求里传入的data变成了 [object object],0.4.6没问题
麻烦看下,崩溃日志:
JsConsole: undefined:914: TypeError: Cannot read property 'split' of null
TypeError: Cannot read property 'split' of null
at AjaxEngine.engine.onload (eval at sr (unknown source), :914:76)
at AjaxEngine._call (eval at sr (unknown source), :533:55)
at eval (eval at sr (unknown source), :650:47)
at Object.request.complete (eval at sr (unknown source), :992:26)
at K.e.callbacks.(anonymous function) (:1:11354)
at Z (:1:10006)
at Object.hr [as processInvokeCallback] (:2:18226)
at Object.createPage.t.processInvokeCallback.tu.(anonymous function) [as execInvokeCallback] (:2:23495)
at global.(anonymous function) (:4:30804)
at :1:1
thirdScriptError
sdk uncaught third Error
__w is not defined
ReferenceError: __w is not defined
在mpvue项目中引入,出现错误
第二行包含第一行的功能。
utils.merge(options.headers, this.config.headers)
utils.merge(options, this.config)
feature:
目前的公共header配置支持动态配置吗?
如果不支持 是否有这个必要支持一下?
还有目前支持设置公共的body吗?
如果不支持是否有必要支持一下?
有英文的话 应该能推广得好一些
能够支持Jsonp吗
拦截器配置如下:
fly.interceptors.response.use(
(response) => {
//只将请求结果的data字段返回
console.log('fly.interceptors response=' + JSON.stringify(response.data));
// return response.data
return Promise.reject(new Error("heheheeh", 99))
},
(err) => {
console.log('fly.interceptors: error message=' + JSON.stringify(err));
}
)
调用如下:
this.$app.$def.fly.get('https://www.sojson.com/open/api/weather/json.shtml?', {
city: '上海'
})
.then(function (response) {
console.log('success result=' + JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
console.log('error msg=' + error.message);
console.log('error status=' + error.status);
});
有时候能进入catch,有时候不行,打断的也是
fly.interceptors.request.use((config, promise) => { if (config.method === 'POST') { let data = new FormData() for (let key in config.body) { data.append(key, config.body[key]) } config.body = data } console.log(config) return config })
我在拦截器里面处理POST数据 但是不能正常工作 request header content-type 还是 json 是哪里有问题吗?
post的在小程序里面为什么Content-Type不是application/x-www-form-urlencoded,而变成了application/json
求教:如果使用fly.get(url,{query:[a,b]})
,出来的请求是url?query=[a,b]
,怎么传data才能转换成
url?query=a&query=b
呢
拦截器
fly.interceptors.request.use((config,promise)=>{
const symbol = config.url.indexOf('?') === -1 ? '?' : '&';
config.url += `${symbol}from=app&appVersion=2.1.1024`;// TODO: 接口需不要需要针对小程序做修改
config.headers["token"] = auth.getToken();
// 修改request
if (config.method === "POST") {
config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
config.body = qs.stringify(config.body);
}
return config;
});
Hi!
Thanks for this great project!
Is it possible to do request cancellation? Axios way seems to be https://github.com/axios/axios#cancellation
Warm regards!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.