javascript - Axios Http 客户端 - 如何使用表单参数构造 Http Post url

标签 javascript node.js axios

我正在尝试创建一个带有一些要设置的表单参数的 postHTTP 请求。我正在使用带有 Node 服务器的 axios。我已经有一个构建 url 的 java 代码实现,如下所示:

JAVA 代码:

HttpPost post = new HttpPost(UriBuilder.fromUri (getProperty("authServerUrl"))
            .path(TOKEN_ACCESS_PATH).build(getProperty("realm")));

List<NameValuePair> formParams = new ArrayList<NameValuePair>();

formParams.add(new NameValuePair("username",getProperty ("username")));
formParams.add(new NameValuePair("password",getProperty ("password")));
formParams.add(new NameValuePair("client_id, "user-client"));

我正在尝试在 axios 中做同样的事情。

AXIOS 实现:

axios.post(authServerUrl +token_access_path,
        {
                username: 'abcd', //gave the values directly for testing
                password: '1235!',
                client_id: 'user-client'
        }).then(function(response) {
            console.log(response); //no output rendered
        }

在 post 请求中设置这些表单参数的方法是否正确?

最佳答案

您必须执行以下操作:

var querystring = require('querystring');
//...
axios.post(authServerUrl + token_access_path,
    querystring.stringify({
            username: 'abcd', //gave the values directly for testing
            password: '1235!',
            client_id: 'user-client'
    }), {
      headers: { 
        "Content-Type": "application/x-www-form-urlencoded"
      }
    }).then(function(response) {
        console.log(response);
    });

关于javascript - Axios Http 客户端 - 如何使用表单参数构造 Http Post url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31756756/

相关文章:

javascript - 拒绝绝对无效的电子邮件地址的最不坏的正则表达式是什么?

javascript - 将鼠标悬停在父 div 上以影响子元素后的伪元素。下划线动画

javascript - Froala 所见即所得编辑器中缺少一些工具栏按钮

javascript - Vue中没有定义axios

javascript - 无法获得 GET 请求 axios 的响应

javascript - Quill:如何防止工具栏滚动并设置高度?

node.js - 剔除中间件

node.js - Skype 机器人以空 body 响应

json数据存入postgresql顺序不同

vue.js - Nuxt 如何在开发或生产中设置 baseURL