uni-app/微信小程序 解析 json 数据(thinkphp6),跨域问题

9 篇文章 0 订阅
6 篇文章 0 订阅

控制器正常查询数据表,建议不要使用分页这些,会有默认封装的各种格式,处理起来可能偏繁琐,注意返回:

直接 return json($data);

不要去搞神马php转换为json之类的,数据会出错,我就是犯了这个低级错误。

<template>
	<view class="page-body">
		<!-- raw_active start -->
		<view v-for="(item, index) in activeArr" :key="index" class="raw_active">
			<image :src="item.ac_img"></image>
			<view class="active-box">
				<img src="../../static/images/Home.png" alt='Home.png' :style="{width: '30rpx',height: '28rpx',marginRight: '10rpx'}">
				<text class="font-34 tit">{{item.ac_name}}</text>
				<span class="btns" @click="turnInfo(item.ac_id)">详情</span>
			</view>			
		</view>
		<!-- raw_active end -->		
	</view>
</template>

<script>
	export default {
		data() {
			return {
			    activeArr: '',
				index: 0
			}
		},
		onLoad () {
		    uni.request({
		        url: 'http://192.168.8.192:802/public/index.php/index/commonmodule/activewgy', //接口地址 
				header: {'content-type':'application/json'},
		        method: 'POST',
				success: (res) => {
				    this.activeArr = res.data  //拿到数据直接使用就是
		            //console.log(res.data)		           
		        }				
		    })
		},
		methods: {    
		    turnInfo (id) {
				uni.navigateTo({
					//url: ''
				})
			}
		}		
	}	
</script>

<style scoped>
</style>

补充一下微信小程序的获取方式

 <view class="raw" wx:for="{{reglist1}}" wx:key="key">
      <view class="raw_list">
        <view class="img_box"><image src="https://xxxxxxxxxxx.com/public/upload/user/{{item.d_img}}"></image></view>
        <view class="txt_box">
          <h3>{{item.d_name}}</h3>                      
          <view class="icon">预约</view>
          <text>{{item.d_goodat}}</text>
        </view>
      </view>
Page({

  /**
   * 页面的初始数据
   */
  data: {
    reglist1: [], //院外专家
    reglist2: []  //院内专家
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     var that = this;
     //发起网络请求
     wx.request({          
      url: 'https://xxxxxxxx.com/public/index.php/index/users/index',
      method: 'POST',      
      dataType: 'json',
      success: function (res){
          //console.log(res.data);
          that.setData({
            reglist1: res.data.info1,
            reglist2: res.data.info2
          });         
          //console.log(reglist1);
      },
      fail: function (res) {
          console.log(res.data);
      }
    })
  }
})

跨域问题,在BaseController.php

 // 初始化
protected function initialize()
{
    //访问XMLHttpRequest已被CORS政策阻止
    //header("Access-Control-Allow-Origin: *");
    // 指定允许其他域名访问  
    header('Access-Control-Allow-Origin:*');
    // 响应类型  
    header('Access-Control-Allow-Methods:*');
    // 响应头设置  
    header('Access-Control-Allow-Headers:*');
    //header('Access-Control-Allow-Headers:x-requested-with,content-type');   
    //允许ajax异步请求带cookie信息
    header('Access-Control-Allow-Credentials:true');
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值