微信小程序:实现跨域请求文件数据(包括php如何引用获取路径参数和传递的参数)

js

const app = getApp()
Page({
  data: {
    
  },
  onLoad(){
    //调用别的域名文件下的文件的aaa方法
    wx.request({
      url: 'https://域名/api/api_wxmini.php?action=aaa',
      data: {
        name:'seven'
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      dataType: 'json',
      success: res => {
        console.log(res.data)
      },
      fail(res) {
        console.log(res)
      }
    })
  }
})

php

<?php
    //判断前端的参数
    if ($_GET['action'] == 'aaa') {
        test();
    }
    //调用方法
    function test(){
       $name = $_POST['name'];  // 获取名为 'name' 的参数值
       echo json_encode($name);
    }
?>

获取路径参数

$_GET['action']

获取小程序端传递的参数 

$_POST['name']

注:微信公众平台需要添加引用的php文件的域名信息

猜你喜欢

转载自blog.csdn.net/weixin_46001736/article/details/134952395