【问题标题】:Posting Data to PHP with Axios使用 Axios 将数据发布到 PHP
【发布时间】:2019-04-12 02:04:44
【问题描述】:

我正在创建一个 Vue 应用程序,我在其中使用 Axios 发布到 PHP。所以我有的是:

methods: {
         onSubmit () {
       axios.post('/wp-content/themes/bones/library/jobsResponse.php',{region: Chicago, jobType: Direct Hire}, {headers: {'X-Requested-With': 'XMLHttpRequest', 'Content-type': 'application/x-www-form-urlencoded'}})
       .then(function(response){
         console.log(response)
       })
       .catch(function(error){console.log(error)})
      },

}

该方法所做的是,当 onSubmit 函数运行时,它将使用 Axios POST 到我创建的名为 jobsResponse.php 的 PHP 文件。它发布的数据是“地区:芝加哥地区,工作类型:直接雇用”。

然后在那个 jobsResponse.php PHP 文件中我有:

<?php
$_POST = json_decode(file_get_contents('php://input'), true);

$region = $_POST['region'];

$jobType = $_POST['jobType'];

echo $region;
echo $jobType;

echo ' this is the posted data content';
?>

这就是使用 Axios 从 Vue 获取发布的数据。在 Axios 中,我正在运行响应的 console.log,当我检查控制台日志时,它会显示我发布到 jobsResponse.php 的数据

如上图所示,我的 Vue 应用程序中的数据已发布到 jobsResponse.php 文件中。所以这一切都很好。我现在需要做的是获取我发布到 jobsResponse.php 文件的数据并在我的 Vue 应用程序中使用它。

所以目前在我的 Vue 应用程序中,我正在使用 PHP 接收 JSON 对象,如下所示:

<?php echo getBhQuery('search','JobOrder','isOpen:true','id,title,categories,dateAdded,externalCategoryID,employmentType,customText12', null, 200, '-dateAdded');?>

我想要做的是在那个 PHP 查询中使用来自 Axios 的发布数据。所以我需要获取发布的数据并以某种方式插入到 PHP 中,例如:

<?php echo getBhQuery('search','JobOrder','isOpen:true AND customText12:"'.$region.'"','id,title,categories,dateAdded,externalCategoryID,employmentType,customText12'); ?>

将 $region 变量添加到 PHP 查询将过滤 JSON 对象以仅拉回具有该变量设置的区域的职位。

所以我有所需的数据发布,这很好。我只是不确定如何获取这些数据并使用它来动态生成一个新的 JSON 对象,就像 ajaxing 一样。

【问题讨论】:

  • 您只是在寻找json_encode()吗?如果没有,我不确定我是否遵循了这个问题。
  • @EdCottrell 我真正想做的是,我正在制作一个从 getBhQuery 中提取 JSON 对象的 Vue 应用程序。我想要做的是使用来自用户输入的数据来更新它。因此,如果用户在选择下拉列表中选择芝加哥地区作为示例,那么我希望使用 Axios 将该数据发布到 jobsResponse.php 文件。我只是在寻找一种方法来获取用户输入数据,使用 Axios 发布它,然后使用该数据更新 getBhQuery 以获取具有附加参数的新 JSON 对象。
  • $_POST 不应该真的那样使用 - 如果你使用 PHP 7.x,我会做这样的事情 -> $post = file_get_contents('php://input') ?? $_POST; $post = json_decode($post, true);

标签: javascript php post vue.js axios


【解决方案1】:

好吧,$_POST 是一个超全局变量,所以我不会覆盖它,因为它们是由 PHP 自动处理的,最好不要管它。

就像提到的评论一样,$post = file_get_contents('php://input') ?? $_POST; 将接收传入的 POST 请求并将其分配给变量 $post。然后,一旦您解析了它:$post = json_decode($post, true); 这会将 JSON 字符串解析为关联的数组。

然后获取您提到的变量$region$jobType

然后您可以将这些插入到您的数据库查询中,或者如果您不进行此生产并且可以拥有固定数据:拥有静态文本文件或将作业列表保存到变量中。

// get the incoming POST data
$post = file_get_contents('php://input') ?? $_POST;

// decode the JSON data
$post = json_decode($post, true);

// assign the needed data from the POST object
$region = $post['region'];

$jobType = $post['jobType'];

// define how you want to filter the data
function $sortJobs($job) use ($region, $jobType)
{
  return ($job['region'] == $region) && ($job['type'] == $jobType);
}

// get your jobs data
$jobs = [[ 'type' => 'x', 'region' => 'minneapolis', 'name' => 'developer'], ['type' => 'y', 'region' => 'chicago', 'name' => 'secretary']];

// filter the jobs data
$filteredJobs = array_filter($jobs, "sortJobs");

// return the filtered data
http_response_code(200);
echo json_encode($filteredJobs);

【讨论】:

    猜你喜欢
    • 2023-02-04
    • 2020-02-05
    • 1970-01-01
    • 2018-02-26
    • 2023-03-26
    • 2020-07-01
    • 2017-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多