在AngularJS中通过HTTP发布JSON数据

时间:2016-02-19 05:00:50

标签: angularjs json http cors

我无法使用正确的内容标题发布我的数据。

var postRecordLikes = function() {
        return $http({
            url: 'http://localhost:8000/api/account/login/',
            method: 'POST',
            contentType: 'application/json',
            data: JSON.stringify({
                'email': 'sd1',
                'password': 'ellllo',
            }),
        });

错误消息:“415(不支持的媒体类型)”。 Content-header说“plain / text”

我也试过这个:

        return $http({
            url: 'http://localhost:8000/api/account/login/',
            method: 'POST',
            headers: {
            'Content-Type': 'application/json',
            },
            data: JSON.stringify({
                'email': 'sd1',
                'password': 'ellllo',
            }),
        });

错误消息:“请求标题字段在预检响应中,Access-Control-Allow-Headers不允许使用Content-Type。”

在我的routes.js中,我有这些设置来阻止CORS问题。

.config(function ($httpProvider) {
    $httpProvider.defaults.headers.common = {};
    $httpProvider.defaults.headers.post = {};
    $httpProvider.defaults.headers.put = {};
    $httpProvider.defaults.headers.patch = {};
})

0 个答案:

没有答案