angularjs post 后台 取不到值,修改contentTye 可以解决

var app = angular.module('maplePointApp', []);
app.config(["$httpProvider", function ($httpProvider) {
    //更改 Content-Type
    $httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8";
    $httpProvider.defaults.headers.post["Accept"] = "*/*";
    $httpProvider.defaults.transformRequest = function (data) {
        //JSON数据转换成字符串形式
        if (data !== undefined) {
            return $.param(data);
        }
        return data;
    };
}])
app.controller('myCtr', function($scope,$http) {
    $scope.initIndex = function () {
        $http({

            method : "post",
            url : "${REQ}/maple/point",
            data : {
                "id" : 123
            }
        }).success(function(rs) {
            console.log(rs)
            $scope.dataList=rs;

        }).error(function(rs) {
        });
    }
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你在 Vue 3 中使用 axios 或者 fetch API 发送 POST 请求传递多个参数,但是在后端无法获这些参数的,可能是因为请求头未设置正确导致的。 在使用 axios 或者 fetch API 发送 POST 请求时,需要设置请求头的 Content-Type 为 application/x-www-form-urlencoded。这样服务器才能正确解析请求体中的参数。 以下是设置请求头的示例代码: ```javascript import axios from 'axios'; const data = { param1: 'value1', param2: 'value2', param3: 'value3' }; axios.post('/api/endpoint', data, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }) .then(response => { console.log(response); }) .catch(error => { console.log(error); }); ``` 在上面的示例中,我们通过在 axios.post() 方法的第三个参数中设置 headers 来设置请求头,其中 Content-Type 设置为 application/x-www-form-urlencoded。 如果你使用的是 fetch API,可以使用 Request 对象来设置请求头。以下是使用 fetch API 发送 POST 请求并设置请求头的示例代码: ```javascript const data = new URLSearchParams(); data.append('param1', 'value1'); data.append('param2', 'value2'); data.append('param3', 'value3'); fetch('/api/endpoint', { method: 'POST', body: data, headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }) .then(response => { console.log(response); }) .catch(error => { console.log(error); }); ``` 在上面的示例中,我们使用了 URLSearchParams 对象来构造请求体,并通过设置 headers 属性来设置请求头。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值