首页 > 解决方案 > 如何使用 Axios 将对象作为 QueryString 传递给 web api

问题描述

我是 Axios 的新手,在这个简单的问题上遇到了问题。

我的反应代码:

const params = {
      "id": "1",
      "name":"Mike"
  };
  axios.request({
      url: 'https://localhost:44343/api/SampleData/test',
      method: 'get',
      data:params
  })

我的 asp.net 核心 web api:

[HttpGet("test")]
public async Task Test([FromQuery]Model model)

我的模型:

public class Model
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }

我未能将 js 对象作为查询字符串传递给我的 api 代码。和model的数据为空。IdName

标签: reactjsaxios

解决方案


我找到了解决方案:

const querystring = require('querystring');

  const params = {
      "id": "1",
      "name":"Mike"
  };
  axios.request({
      url: 'https://localhost:44343/api/SampleData/test?' + querystring.stringify(params),
      method: 'get',
      data:params
  })

推荐阅读