【问题标题】:angularjs 400 bad request during http post requesthttp post请求期间的angularjs 400错误请求
【发布时间】:2017-06-29 11:52:36
【问题描述】:

我正在构建一个带有 Django Rest Framework API 后端的单页 Web 应用程序。我在发送注册表时遇到问题。我认为日期的格式存在问题。 这是控制器的外观:

(function () {
'use strict';

angular
    .module('CityWits')
    .controller('standardController', standardController);

standardController.$inject = ['UserService','$location', '$rootScope', 'FlashService'];
function standardController(UserService, $location, $rootScope, FlashService) {
    var vm = this;

    vm.standard = standard;

    function standard() {
        vm.dataLoading = true;

        var str = [];
        for(var p in vm.user){
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(vm.user[p]));
        }
        str = str.join("&");

        UserService.CreateStandard(vm.user)
            .then(function (response) {
                if (response.success) {
                    FlashService.Success('Registration successful', true);
                    $location.path('/sign-in');
                } else {
                    alert(response.message)
                    vm.dataLoading = false;
                }
            });
    }

    function fixDate(date){


    }
}

})();

帖子请求:

 function CreateStandard(user) {
        console.log(user)
        return $http.post('http://127.0.0.1:8000/api/users/standard', user).then(handleSuccess, handleError('Error creating user'));
    }

表单发送的json对象:

Object

出生日期 : 1993 年 8 月 6 日星期五 00:00:00 GMT-0400(东部夏令时间) 名 : “大卫” 性别 : “米” 姓 : “加洛韦” 用户 : 目的 电子邮件 : “dcgallo93@gmail.com” 密码 : “*****” 原型 : 目的 原型 : 对象

【问题讨论】:

    标签: angularjs post request django-rest-framework


    【解决方案1】:

    如果您想发布为 JSON 字符串,您必须将 HTTP 标头“内容类型”设置为 JSON。

    否则,数据需要采用 application/x-www-form-urlencoded 的形式,即:parameter=value&also=another >

    这在 DRF 文档中突出显示 here

    注意:在开发客户端应用程序时,请务必记住确保 在 HTTP 中发送数据时,您正在设置 Content-Type 标头 请求。

    如果您不设置内容类型,大多数客户端将默认使用 'application/x-www-form-urlencoded',这可能不是你想要的。

    例如,如果您使用 jQuery 发送 json 编码数据 .ajax() 方法,你应该确保包含 contentType: “应用程序/json”设置。

    坦率地说,JSON 更清晰,所以我会做的是:

    function CreateStandard(user) {
        return $http({
            method: 'POST', 
            url: 'http://127.0.0.1:8000/api/users/standard', 
            data: JSON.stringify(user),
            headers: {"Content-Type": "application/json"}
        }).then(...);
    }
    

    【讨论】:

    • 请您看看this的问题好吗?我已经执行了这些建议,但仍然收到错误...
    【解决方案2】:

    使用JSON.stringify 发送user 对象:

    function CreateStandard(user) {
        console.log(user)
        return $http.post('http://127.0.0.1:8000/api/users/standard', JSON.stringify(user))
                    .then(handleSuccess, handleError('Error creating user'));
    }
    

    【讨论】:

    • 试过了,还是发回 400 bad request
    • 请您看看this的问题好吗?我已经执行了这些建议,但仍然收到错误...
    • @DavidGalloway 你找到解决这个错误请求问题的方法了吗?
    猜你喜欢
    • 1970-01-01
    • 2019-07-17
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多