【问题标题】:Method Post with json - Angular js使用 json 发布方法 - Angular js
【发布时间】:2015-07-15 18:05:35
【问题描述】:

我的请求有问题,服务器等待:

*POST /work/v1/chats/ HTTP/1.1
X-Client-Instance: clienteinstance
Content-Type: multipart/form-data; boundary=32366fff-0f80-474c-a809-14e6ffaacca1
Content-Length: 352
User-Agent: Dalvik/2.1.0 (Linux; U; Android 5.0.2; XT1033 Build/LXB22.46-28)
Host: XXXXXX
Connection: Keep-Alive
Accept-Encoding: gzip
--32366fff-0f80-474c-a809-14e6ffaacca1
Content-Disposition: form-data; name="message-body"
Content-Type: application/json; charset=UTF-8
Content-Length: 110
Content-Transfer-Encoding: binary
{"attachment":"","content":"Test","localAddress":"XXXXXX","receiver":"XXXXX"}
--32366fff-0f80-474c-a809-14e6ffaacca1--*
500 Internal Server Error
Connection: keep-alive
X-Powered-By: Undertow/1
Server: WildFly/8
Content-Type: text/plain
Content-Length: 71
Date: Wed, 15 Jul 2015 17:15:10 GMT


HTTP/1.1 200 OK
Connection: keep-alive
X-Powered-By: Undertow/1
Server: WildFly/8
Transfer-Encoding: chunked
Content-Type: application/json
Date: Tue, 14 Jul 2015 13:56:48 GMT

在我的请求中我发送:

POST /work/v1/chats HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0
Accept: application/json, text/plain, */*
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
X-Client-Instance: client instance
Content-Type: multipart/form-data; charset=UTF-8
Referer: http://localhost:8080/uc/
Content-Length: 120
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

{"data":{"attachment":"","content":"Test","localAddress":"XXXXXX","receiver":"XXXXX"}}HTTP/1.1 500 Internal Server Error
Connection: keep-alive
X-Powered-By: Undertow/1
Server: WildFly/8
Content-Type: text/plain
Content-Length: 71
Date: Wed, 15 Jul 2015 17:15:10 GMT

如何添加body?在我的请求中?

Content-Disposition: form-data; name="message-body"
Content-Type: application/json; charset=UTF-8
Content-Length: 110
Content-Transfer-Encoding: binary
{"attachment":"","content":"Test","localAddress":"XXXXXX","receiver":"XXXXX"}
--32366fff-0f80-474c-a809-14e6ffaacca1--*

代码:

	$scope.sendMsg = function(){

		
	$http.post('http://localhost:8080/work/v1/chats', {data: 
				{attachment:"",
				 content:"Test",
				 localAddress:"192.168.90.244",
				 receiver:"XXX"}}, { headers: {'X-Client-Instance': 'XXXX', 'Content-Type': 'multipart/form-data'}}  )
.success( function(data)
 {
    alert("success!");
 })
 .error( function( data)
 {
   alert("error!");
 });
	
	
};

【问题讨论】:

  • 这有点令人困惑。请用语言正确解释您的问题所在。在一个地方你设置了一个 ContentType 而在另一个地方你想要一个不同的。如果你想发送 formData 使用formData api

标签: json angularjs http


【解决方案1】:

试试 JSON.stringify

                   body= $.param({
                        params: JSON.stringify({
                            {   attachment:"",
                                content:"Test",
                                localAddress:"192.168.90.244",
                                receiver:"XXX"}
                        });
$http.post('http://localhost:8080/work/v1/chats', body , { headers: {'X-Client-Instance': 'XXXX', 'Content-Type': 'multipart/form-data'}}  )
.success( function(data)
 {
alert("success!");
 })
.error( function( data)
{
alert("error!");
});
};

或将值作为 FORM 发布,添加标头和 transformRequest

 headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
    var str = [];
    for(var p in obj)
    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
    return str.join("&");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 2013-09-15
    • 2020-05-05
    • 2023-03-11
    相关资源
    最近更新 更多