爱玺玺

爱玺玺的生活日记本。wx:lb87626

微信小程序调用自己服务器上的数据

index.js

//index.js

const app = getApp()


Page({

data:{

list: []

},

onLoad: function () { //这个onLoad注意大小写区分,刚开始用小写总报错

var that = this;

wx.request({

url: 'http://test.jianhaozhan.cn/getjson.php',//这个地址好像只能https的

method: "GET",

header: {

'Content-Type':

'json'

},

success: function (res) {

console.log(res.data);

var date = res.data;

that.setData({

list: date

})

},

fail: function () {

console.log("接口调用失败");

}

})

}


})



index.wxml


<scroll-view class='list'>

  <view class='item' wx:for="{{list}}" wx:for-item="item" wx:key="key">

    <image src='http://tszyzx.com{{item.img}}'></image>

  </view>

</scroll-view>


index.wxss 样式文件不是这里的关键

.item{

float: left;

padding:12rpx 13rpx 12rpx 11rpx;

}


.item image{

width: 352rpx;

height: 194rpx;

display: block;

padding:0px;

background:#eee;

}


服务器 getjson.php

<?php 

$con = mysql_connect("localhost","taoyong","k328b5zg");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }


mysql_select_db("taoyong", $con);

$result=mysql_query("select img from imgs order by id desc");

while ($row=mysql_fetch_array($result)) {

$results[] = $row;

}

echo $jn=json_encode($results);

?>



数据库表:建一个表imgs 下面的数据可能会很快失效。

/*

Navicat MySQL Data Transfer


Source Server         : meizhanmei

Source Server Version : 50540

Source Host           : localhost:3306

Source Database       : tszy


Target Server Type    : MYSQL

Target Server Version : 50540

File Encoding         : 65001


Date: 2018-10-10 17:23:33

*/


SET FOREIGN_KEY_CHECKS=0;


-- ----------------------------

-- Table structure for imgs

-- ----------------------------

DROP TABLE IF EXISTS `imgs`;

CREATE TABLE `imgs` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `img` varchar(600) DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=MyISAM AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;


-- ----------------------------

-- Records of imgs

-- ----------------------------

INSERT INTO `imgs` VALUES ('2', '/uploads/180313/1-1P313092I1928.jpg');

INSERT INTO `imgs` VALUES ('3', '/uploads/allimg/180322/4-1P3221442370-L.jpg');

INSERT INTO `imgs` VALUES ('4', '/uploads/allimg/180404/4-1P404161Q00-L.jpg');

INSERT INTO `imgs` VALUES ('5', '/uploads/allimg/180509/7-1P5091634250-L.jpg');

INSERT INTO `imgs` VALUES ('6', '/uploads/allimg/180510/7-1P5100Z6420-L.jpg');

INSERT INTO `imgs` VALUES ('7', '/uploads/180616/7-1P616161512428.jpg');

INSERT INTO `imgs` VALUES ('8', '/uploads/allimg/180316/4-1P3161402150-L.jpg');

INSERT INTO `imgs` VALUES ('9', '/uploads/180313/1-1P313093HR52.jpg');

INSERT INTO `imgs` VALUES ('10', '/uploads/180205/4-1P2051A400353.jpg');

INSERT INTO `imgs` VALUES ('11', '/uploads/allimg/180202/4-1P202152H80-L.jpg');

INSERT INTO `imgs` VALUES ('12', '/uploads/171123/4-1G219140413353.jpg');

INSERT INTO `imgs` VALUES ('13', '/uploads/171123/4-1G21915405K10.jpg');

INSERT INTO `imgs` VALUES ('14', '/uploads/allimg/180202/4-1P2021412110-L.jpg');

INSERT INTO `imgs` VALUES ('15', '/uploads/allimg/180202/4-1P2021435470-L.jpg');

INSERT INTO `imgs` VALUES ('16', '/uploads/allimg/180202/4-1P2021453570-L.jpg');

INSERT INTO `imgs` VALUES ('17', '/uploads/allimg/180202/4-1P2021504190-L.jpg');

INSERT INTO `imgs` VALUES ('18', '/uploads/allimg/180202/4-1P2021515590-L.jpg');

INSERT INTO `imgs` VALUES ('19', '/uploads/171123/2-1G12314545W42.jpg');


发表评论:

Powered By Z-BlogPHP 1.4 Deeplue Build 150101

Copyright Your WebSite.Some Rights Reserved.

蜀ICP备11021721号-5