Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vue.js 我可以将Vue native与mysql一起使用吗_Vue.js_Mobile_Vue Native - Fatal编程技术网

Vue.js 我可以将Vue native与mysql一起使用吗

Vue.js 我可以将Vue native与mysql一起使用吗,vue.js,mobile,vue-native,Vue.js,Mobile,Vue Native,大家好,我正在尝试开发一个android应用程序 我尝试使用axios通过php将应用程序连接到远程mysql服务器(当我在web上使用vuejs运行代码时,它会工作) 以下是Vue本机代码 fetchAllData:function(){ axios.post('db.php', { action:'fetchall' }).then(function(response){ app.allData = response.data;

大家好,我正在尝试开发一个android应用程序

我尝试使用axios通过php将应用程序连接到远程mysql服务器(当我在web上使用vuejs运行代码时,它会工作)

以下是Vue本机代码

      fetchAllData:function(){
      axios.post('db.php', {
      action:'fetchall'
      }).then(function(response){
      app.allData = response.data;
      });
以下是db.php文件:

$received_data = json_decode(file_get_contents("php://input"));
$data = array();
if($received_data->action == 'fetchall')
{
 $query = "
 SELECT * FROM users ";
 $statement = $connect->prepare($query);
 $statement->execute();
 while($row = $statement->fetch(PDO::FETCH_ASSOC))
 {
  $data[] = $row;
 }
 echo json_encode($data);
}
下面是错误:


您的错误是网络错误,表示您没有internet连接或无法访问端点

您正在尝试连接到有效的终结点吗

您可以在axios中设置端点,如下所示:

export const apiParams = {
  baseURL: 'YOUR URL HERE',
  timeout: 20000,
  withCredentials: true
}
const api = axios.create(apiParams)
methods: {
      async fetchAllData() {
        try {
          await axios.post('db.php', {
            action: 'fetchall'
          })
          app.allData = response.data
        } catch (error) {
          console.log(error)
        }
      }
    },
导出后,您可以轻松使用

api.post('db.php', { data });
基本上,未处理的承诺拒绝就是说axios.post是一个承诺,如果它被拒绝,您应该处理它的错误

尝试将axios.post放在Try-catch块中,您可以像这样使用ES6 async/await:

export const apiParams = {
  baseURL: 'YOUR URL HERE',
  timeout: 20000,
  withCredentials: true
}
const api = axios.create(apiParams)
methods: {
      async fetchAllData() {
        try {
          await axios.post('db.php', {
            action: 'fetchall'
          })
          app.allData = response.data
        } catch (error) {
          console.log(error)
        }
      }
    },
或者您可以使用旧式的错误处理方式来处理catch,但我不建议使用它,如果您有一系列承诺,最好使用上面的示例中的async/await

methods: {
      fetchAllData() {
        axios
          .post('db.php', {
            action: 'fetchall'
          })
          .then(function(response) {
            app.allData = response.data
          })
          .catch(err => {
            console.log(err)
          })
      }
    },
阅读有关使用then/catch处理承诺的信息 异步等待:


在您解决网络错误之前,承诺拒绝应该消失,但您的控制台日志中将有一个已处理的错误。

谢谢您的回答!但它并没有为我工作,我仍然得到网络错误(我的互联网连接是良好和稳定的)可能是关于php文件?android可以运行php请求吗?试着在使用axios.post时放入整个url,就像这次一样,我得到了500个错误代码,我尝试在手机上打开应用程序(不是在模拟器上),我又得到了500个错误代码,之后我尝试在chrome浏览器中浏览到服务器\u url/db.php,我又得到了500个错误代码:(好的,很好,这意味着错误现在在PHP端,vue本机端没有错误,这意味着vue本机部分已解决!我猜$received_data->action不是对象,因为json_decode正在返回数组。请尝试$received_data[“action”],首先不要使用任何查询,只需传回一个简单字符串。如果有效,请一步一步地进行,直到您可以将数据从mysql传回应用程序。如果有帮助,请告诉我。仍然不起作用。我从4天以来一直在处理它,我真的很累,您能做一个演示吗?