php - 使用 Axios 和 CodeIgniter 发送带有 JSON 的 POST 请求

标签 php ajax axios

我正在尝试使用 Axios 发送数据:

axios.post( url,JSON.stringify({'i': '90'}))
    .then(function (response) {
        console.log(response);
    });

并在服务器上获取它:

var_dump(($this->input->post())); // Returns an array |  $_POST

对于上面的 JSON 值,我得到了这样的响应:

array(2) { ["{"i":"90"}"]=> string(0) "" [0]=> string(0) "" }

没有 JSON.stringify,结果来自 var_dump(($this->input->post()));$_POST 是空数组。

如何用Axios发送带有JSON数据的POST请求,并用PHP在服务器上获取?

最佳答案

我遇到了同样的问题,最后发现是因为XSS Filtering, 使用

   $i = $this->input->post("i",false);

在 axios 中使用 so

var formdata=new FormData();
    formdata.append("key",value);

    this.axios.post("http://URL",formdata).then(res=>{})

关于php - 使用 Axios 和 CodeIgniter 发送带有 JSON 的 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53974284/

相关文章:

ruby-on-rails - 使用 Ruby On Rails 在 AJAX 中进行星级评定

php - 在模态窗口和ajax请求中显示轮播

javascript - 在 Promise.all 中使用 .map

php - 在没有子域的子文件夹中安装多个 laravel 项目

javascript - 变量赋值给输入值

php - is_file 或 file_exists 在 PHP

php - 使用 AJAX 将表单值提交到数据库

reactjs - ReactJS 中没有“Access-Control-Allow-Origin”

reactjs - react /Axios : fetch API data with key for CoinMarketCap

php - 如何在 URL 中传递斜杠/?