我调用一登录接口,如果返回值status=401的场景下,会不断收到来自客户端的请求,这个问题100%发生。那位大侠可指点一下。通过console.log,xhr.send只执行了一次。
post: function(params) {
var xhr = new plus.net.XMLHttpRequest()
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status == 200) {
return params.success(JSON.parse(xhr.responseText))
} else {
return params.error(xhr.status, JSON.parse(xhr.responseText))
}
}
}
if(params.timeout){
xhr.timeout = params.timeout
}
xhr.open('POST', MiniUtil.createAPI(params.url))
xhr.setRequestHeader('Content-Type', 'application/json')
if (params.header) {
for (var key in params.header) {
xhr.setRequestHeader(key, params.header[key])
}
}
//通过console.log,这里只执行了一次。
xhr.send(JSON.stringify(params.data))
}
下面是服务器端日志信息
--> POST /api/v1/oauth2/token 401 11ms -
<-- POST /api/v1/oauth2/token
111111
{ name: 'a b c',
password: 'qqqqqqqq',
device_name: 'iOS',
client_id: 'Lt7hPcA6nuX38FY4',
client_secret: 'vV2RpBsZBE4pNGG2' }
OAuth2Error: Client credentials are invalid
at OAuth2Error (/home/data/demo.minicloud.io/node_modules/koa-oauth-server/node_modules/oauth2-server/lib/error.js:30:12)
at /home/data/demo.minicloud.io/node_modules/koa-oauth-server/node_modules/oauth2-server/lib/grant.js:147:19
at /home/data/demo.minicloud.io/lib/router/api/oauth2/model.js:68:24
at GeneratorFunctionPrototype.next (native)
at onFulfilled (/home/data/demo.minicloud.io/node_modules/co/index.js:64:19)
at process._tickCallback (node.js:353:9)
--> POST /api/v1/oauth2/token 401 8ms -
<-- POST /api/v1/oauth2/token
111111
{ name: 'a b c',
password: 'qqqqqqqq',
device_name: 'iOS',
client_id: 'Lt7hPcA6nuX38FY4',
client_secret: 'vV2RpBsZBE4pNGG2' }
OAuth2Error: Client credentials are invalid
at OAuth2Error (/home/data/demo.minicloud.io/node_modules/koa-oauth-server/node_modules/oauth2-server/lib/error.js:30:12)
at /home/data/demo.minicloud.io/node_modules/koa-oauth-server/node_modules/oauth2-server/lib/grant.js:147:19
at /home/data/demo.minicloud.io/lib/router/api/oauth2/model.js:68:24
at GeneratorFunctionPrototype.next (native)
at onFulfilled (/home/data/demo.minicloud.io/node_modules/co/index.js:64:19)
at process._tickCallback (node.js:353:9)
2 个回复
DCloud_IOS_XTY
plus.net.XMLHttpRequest不会自动重发请求,可以抓下包看看到底是哪端的问题
d***@163.com
mui新手。
不过,通常来说 返回 401给客户端 时候 不是应该带 token 吗?
server client
<--------任意http请求(旧token)-------------
----------------401------------------------------> //token超时
<---------重新登录(user/passwrod)--------
-------------------token------------------------->
<--------重发http请求(新token)-------------
---------------------200-------------------------->