TouchUI,API里【数据、网络、文件】怎么将token存到header中?


无标题.png

像postman调接口将token存在Headers中,

无标题2.png

在touchUI中怎么将token存在Headers中?
直接写在header中报
Response for preflight has invalid HTTP status code 403
 
请问怎么处理?
已邀请:

admin

赞同来自:

发一下实现代码

zengwei

赞同来自:

<script>
const requestUrl = ''
export default {
config: {
"navigationBarTitleText": "发起请求",
"pages": [
],
"theme": {
"theme-color": "#3399ff"
},
"window": {
"scrollType": "div",
"navigationBarBackgroundColor":"#ffffff",
"navigationBarTextStyle":"black",
"navigationBarBorderColor":"rgba(231, 231, 231, 0.6)"
},
"networkBaseUrl":""
},
data () {
return {
loading: false,
res: ''
}
},
methods: {
makeRequest() {
var self = this
self.loading = true
var headerValue = {
'content-type': 'application/x-www-form-urlencoded'
}
headerValue.token = '1111111111111'

ui.request({
url: requestUrl,
data: {
},
header: headerValue,
method: 'POST',
success: function (result) {
self.loading = false
console.log('request success', result)
self.res = JSON.stringify(result, null, 4)
},
fail: function ({ errMsg }) {
console.log('request fail', errMsg)
self.loading = false
}
})
}
},
mounted () {

}
}
</script>

<template>
<ui-view id="ui-app">
<ui-view class="page-body">
<ui-view class="page-body-wording">
<ui-view class="page-body-div">
点击向服务器发起请求
</ui-view>
<ui-view class="code">
<ui-view class="title">服务器返回:</ui-view>
<pre>{{res}}</pre>
</ui-view>
</ui-view>
<ui-view class="btn-area">
<ui-button bindtap="makeRequest" type="primary" disabled="{{loading}}" loading="{{loading}}">request</ui-button>
</ui-view>
</ui-view>
</ui-view>
</template>

admin

赞同来自:

微信图片_20180615174213.jpg

我们用这段代码测试没问题。估计是服务端问题

zengwei

赞同来自:

这是postman访问的正确显示

无标题.png

 这是页面访问的错误提示

无标题2.png

 这是实现的代码
<script>
const requestUrl = 'http://api.xlceb.com/test-repairManWeb/api/v1.0.0/queryNearbyCar'
// const requestUrl = 'http://bbs.touchui.io/question/153'
export default {
config: {
"navigationBarTitleText": "发起请求",
"pages": [
],
"theme": {
"theme-color": "#3399ff"
},
"window": {
"scrollType": "div",
"navigationBarBackgroundColor":"#ffffff",
"navigationBarTextStyle":"black",
"navigationBarBorderColor":"rgba(231, 231, 231, 0.6)"
},
"networkBaseUrl":""
},
data () {
return {
loading: false,
res: ''
}
},
methods: {
makeRequest() {
var self = this
self.loading = true
var headerValue = {
'content-type': 'application/x-www-form-urlencoded'
}
headerValue.token = '7df7f5b4-01cb-443e-bb1b-084ed628a6f3'

ui.request({
url: requestUrl,
data: {
},
header: headerValue,
method: 'POST',
success: function (result) {
self.loading = false
console.log('request success', result)
self.res = JSON.stringify(result, null, 4)
},
fail: function ({ errMsg }) {
console.log('request fail', errMsg)
self.loading = false
}
})
}
},
mounted () {

}
}
</script>

<template>
<ui-view id="ui-app">
<ui-view class="page-body">
<ui-view class="page-body-wording">
<ui-view class="page-body-div">
点击向服务器发起请求
</ui-view>
<ui-view class="code">
<ui-view class="title">服务器返回:</ui-view>
<pre>{{res}}</pre>
</ui-view>
</ui-view>
<ui-view class="btn-area">
<ui-button bindtap="makeRequest" type="primary" disabled="{{loading}}" loading="{{loading}}">request</ui-button>
</ui-view>
</ui-view>
</ui-view>
</template>



<style lang="less">
@import './static/css/animate.css';
@import './static/css/loading.less';
@import './static/font/iconfont.css';
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

body {
font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', SimSun, sans-serif;
-webkit-touch-callout: none;
}

html,
body {
font-size: 12px;
}

#ui-app {
width: 100%;
height: 100%;
}

.touchui-hover{
background-color: #eee !important;
}
.iphonex-bottom{
height: env(safe-area-inset-bottom);
background-color: #fff;
width:100%;
}
.ui-nav-bar {
top: 0;
left: 0;
width: 100%;
.nav-bar-left, .nav-bar-right {
position: relative;
width: 66px;
text-align: center;
}
.back {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
i {
margin-left: 10px;
}
}
.nav-bar-center-text{
.mix-text-overflow();
}
}

.ui-tab-bar {
&.div-scroll {
position: absolute;
}
&.body-scroll {
position: fixed;
}
}

.router-view {
width: 100%;
}
</style>
你好,请帮我看一下

admin

赞同来自:

我们测了一下,在其他地方使用你的代码,不加token返回200,加了token返回403.
如果在你们官方页面下使用这段代码(避开跨域问题),加了token返回200
所以可以确定是你们后端逻辑写的有问题

admin

赞同来自:

你们后端逻辑是这样:如果没有token,不对跨域限制。请求有token,则禁止跨域请求

微信图片_20180619104309.jpg

 

要回复问题请先登录注册



QQ:378966107

邮箱:frank@uileader.com

微信进交流群 加小助手微信进交流群