首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Angular,内容类型未与$http一起发送

Angular,内容类型未与$http一起发送
EN

Stack Overflow用户
提问于 2013-04-24 22:19:00
回答 5查看 105.5K关注 0票数 60

Angular没有添加正确的内容类型选项,我尝试了以下命令:

代码语言:javascript
复制
$http({
    url: "http://localhost:8080/example/teste",
    dataType: "json",
    method: "POST",
    headers: {
        "Content-Type": "application/json"
    }
}).success(function(response){
    $scope.response = response;
}).error(function(error){
    $scope.error = error;
});

上面的代码生成以下http请求:

代码语言:javascript
复制
POST http://localhost:8080/example/teste HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 0
Cache-Control: no-cache
Pragma: no-cache
Origin: http://localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
Content-Type: application/xml
Accept: application/json, text/plain, */*
X-Requested-With: XMLHttpRequest
Referer: http://localhost:8080/example/index
Accept-Encoding: gzip,deflate,sdch
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: JSESSIONID=C404CE2DA653136971DD1A3C3EB3725B

如您所见,内容类型不是"application/json",而是"application/xml“。我是不是漏掉了什么?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-04-24 22:49:15

您需要在请求中包含一个正文。否则,Angular将删除内容类型标头。

$http的参数中添加data: ''

票数 95
EN

Stack Overflow用户

发布于 2013-04-25 00:05:40

代码语言:javascript
复制
$http({
    url: 'http://localhost:8080/example/teste',
    dataType: 'json',
    method: 'POST',
    data: '',
    headers: {
        "Content-Type": "application/json"
    }

}).success(function(response){
    $scope.response = response;
}).error(function(error){
    $scope.error = error;
});

试着这样做。

票数 30
EN

Stack Overflow用户

发布于 2016-04-28 22:20:30

代码语言:javascript
复制
         $http({
                method: 'GET',
                url:'/http://localhost:8080/example/test' + toto,
                data: '',
                headers: {
                    'Content-Type': 'application/json'
                }
            }).then(
                function(response) {
                    return response.data;
                }, 
                function(errResponse) {
                    console.error('Error !!');
                    return $q.reject(errResponse);
                }
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16194442

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档