关于使用$httpPOSTContent-Type应用程序/x-www-form-urlencoded访问API和http应用于的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于@JsonP
关于使用$ http POST Content-Type应用程序/ x-www-form-urlencoded访问API和http应用于的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于@JsonProperty无法用于Content-Type:application / x-www-form-urlencoded、@RequestBody接收Content-type为application/x-www-form-urlencoded字符串乱码、angular – 使用x-www-form-urlencoded的HttpClient POST请求、angularjs – 使用$http POST Content-Type application / x-www-form-urlencoded访问API等相关知识的信息别忘了在本站进行查找喔。
本文目录一览:- 使用$ http POST Content-Type应用程序/ x-www-form-urlencoded访问API(http应用于)
- @JsonProperty无法用于Content-Type:application / x-www-form-urlencoded
- @RequestBody接收Content-type为application/x-www-form-urlencoded字符串乱码
- angular – 使用x-www-form-urlencoded的HttpClient POST请求
- angularjs – 使用$http POST Content-Type application / x-www-form-urlencoded访问API
使用$ http POST Content-Type应用程序/ x-www-form-urlencoded访问API(http应用于)
我想上网本REST API,它接受三个参数: stationId
,crusherId
,monthYear
我做它像这样在AngularJS为:
$http({ //headers: {''Content-Type'':''application/x-www-form-urlencoded; charset=UTF-8''}, //headers: {''Content-Type'': ''application/json; charset=UTF-8''}, headers: { ''Content-Type'': ''application/x-www-form-urlencoded; charset=UTF-8'', ''Accept'': ''application/json'' }, url: ''https://myurl../api/getHPData'', method: ''POST'', data: { stationId: 263, crusherId: 27, monthYear: ''2016-4'' } }) .then(function(data, status, headers, config) { //console.log(JSON.stringify(response)); console.log(data); }) .catch(function(error){ //console.log("Error: " + JSON.stringify(error)); console.log(error); })
但是我总是这样:
对象{数据:“ {”结果“:”假“}”“,状态:200,配置:对象,状态文本:”确定“,标头:功能}
要么
{“ data”:“ {\” result \“:\” false \“}”,“状态”:200,“ config”:{“方法”:“ POST”,“
transformRequest”:[null],“ transformResponse “:[null],”标头“:{” Content-
Type“:” application / x-www-form-urlencoded; charset = UTF-8“,” Accept“:”
application / json“},” url“: “
https://myurl../api/getHPData ”,“ data”:{“
stationId”:263,“ crusherId”:27,“ monthYear”:“ 2016-4”}},“ statusText”:“ OK”
}
如果我更改header
Content-Type
为:
headers: {''Content-Type'': ''application/json; charset=UTF-8''},
它给:
对象{数据:null,状态:-1,配置:对象,statusText:“”,标头:函数}
要么
{“数据”:null,“状态”:-1,“配置”:{“方法”:“ POST”,“ transformRequest”:[null],“
transformResponse”:[null],“标题”:{“内容-Type“:” application / json; charset =
UTF-8“,” Accept“:” application / json,text / plain, / “},” url“:”
https://myurl../api/getHPData “ ,“数据”:{“
stationId”:263,“ crusherId”:27,“ monthYear”:“ 2016-4”}},“ statusText”:“”}
我做错了,请帮帮我。
柱塞在这里:
https://plnkr.co/edit/57SiCdBZB2OkhdR03VOs?p=preview
(编辑)
注意: 我可以这样做jQuery
:
<script>$(document).ready(function() { get_homepage_data(263, 27, ''2016-04''); function get_homepage_data(stationIds, crusherIds, date) { var url = "https://myurl../api/getHPData"; var data_to_send = { ''stationId'': stationIds, ''crusherId'': crusherIds, ''monthYear'': date }; console.log("Value is: " + JSON.stringify(data_to_send)); //change sender name with account holder name // console.log(data_to_send) $.ajax({ url: url, method: ''post'', dataType: ''json'', //contentType: ''application/json'', data: data_to_send, processData: true, // crossDomain: true, beforeSend: function () { } , complete: function () {} , success: function (result1) { var Result = JSON.parse(result1); var value_data = Result["valueResult"]; var foo = value_data["gyydt"]; console.log("Log of foo is: " + foo); var foo2 = 0; // 10 lac is one million. foo2 = foo / 1000000 + '' million''; console.log(JSON.stringify(value_data["gyydt"]) + " in million is: " + foo2); } , error: function (request, error) { return false; } }); } }); // eof Document. Ready </script>
上面脚本的输出script
是:
- 值是:{“ stationId”:263,“ crusherId”:27,“ monthYear”:“ 2016-04”}
- XHR完成加载:POST“ https://myurl../api/getHPData ”。
- foo的日志是:26862094
- 以百万为单位的“ 26862094”是:26862.94万
这是完美的。:)
答案1
小编典典发布使用URL编码的表单数据时,请使用$
httpParamSerializer服务转换请求:
$http({ headers: {''Content-Type'':''application/x-www-form-urlencoded; charset=UTF-8''}, url: ''https://fnrc.gov.ae/roayaservices/api/getHPData'', method: ''POST'', transformRequest: $httpParamSerializer, transformResponse: function (x) { return angular.fromJson(angular.fromJson(x)); }, data: { "stationId": 263, "crusherId": 27, "monthYear": ''2016-04'' }}) .then(function(response) { console.log(response); $scope.res = response.data; console.log($scope.res);});
通常,$
http服务会自动解析JSON编码对象的结果,但是此API返回的是已从对象进行双重序列化的字符串。该transformResponse
功能解决了该问题。
PLNKR上的演示
@JsonProperty无法用于Content-Type:application / x-www-form-urlencoded
REST API映射到Java对象时,采用输入内容类型:application / x-www-form-urlencoded
public class MyRequest {
@JsonProperty("my_name")
private String myName;
@JsonProperty("my_phone")
private String myPhone;
//Getters and Setters of myName and myPhone.
}
在表单输入请求中,我正在设置my_name和my_phone的值,但是MyRequest对象带有myName和myPhone作为空值。
我正在使用Jackson批注2.3 jar
有什么建议可能有什么问题吗?
@RequestBody接收Content-type为application/x-www-form-urlencoded字符串乱码
当发送方发送请求content-type为application/x-www-form-urlencoded,后端采用@RequestBody String body接收字符串出现乱码时,应在方法体加入如下代码即可:
@RequestMapping(value = "/tradeResult")
public void tradeResult(@RequestBody String body) throws Exception {
body = URLDecoder.decode(body, StandardCharsets.UTF_8.name());
}
angular – 使用x-www-form-urlencoded的HttpClient POST请求
login(username,password): Observable<any> { return this.http.post('/login',{ username: username,password: password },{ headers: new HttpHeaders() .set('Content-Type','x-www-form-urlencoded') } );
不幸的是我的API说我发送了空的用户名和密码。
所以我决定向我的登录端点发一个邮递员请求,看看问题来自哪里,邮递员请求确实返回了用户名和密码。
为什么当我从邮递员发帖时我的API返回我的用户名和密码,当我从我的Angular应用程序发布时,我的API返回空值?有什么我想念的吗?
下面的代码段应该可行。
login(username,password): Observable<any> { const body = new HttpParams() .set('username',username) .set('password',password); return this.http.post('/login',body.toString(),{ headers: new HttpHeaders() .set('Content-Type','application/x-www-form-urlencoded') } ); }
angularjs – 使用$http POST Content-Type application / x-www-form-urlencoded访问API
stationId,crusherId,monthYear
我在AngularJS中这样做:
$http({ //headers: {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'},//headers: {'Content-Type': 'application/json; charset=UTF-8'},headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8','Accept': 'application/json' },url: 'https://myurl../api/getHPData',method: 'POST',data: { stationId: 263,crusherId: 27,monthYear: '2016-4' } }) .then(function(data,status,headers,config) { //console.log(JSON.stringify(response)); console.log(data); }) .catch(function(error){ //console.log("Error: " + JSON.stringify(error)); console.log(error); })
但我总是得到这个:
Object {data: “{“result”:”false”}”,status: 200,config: Object,statusText: “OK”,headers: function}
要么
{“data”:”{\”result\”:\”false\”}”,”status”:200,”config”:{“method”:”POST”,”transformRequest”:[null],”transformResponse”:[null],”headers”:{“Content-Type”:”application/x-www-form-urlencoded;
charset=UTF-8″,”Accept”:”application/json”},”url”:”07000″,”data”:{“stationId”:263,”crusherId”:27,”monthYear”:”2016-4″}},”statusText”:”OK”}
如果我将标题Content-Type更改为:
headers: {'Content-Type': 'application/json; charset=UTF-8'},
它给:
Object {data: null,status: -1,statusText: “”,headers: function}
要么
{“data”:null,”status”:-1,”headers”:{“Content-Type”:”application/json;
charset=UTF-8″,”Accept”:”application/json,text/plain,
/“},”statusText”:””}
我做错了什么,请帮助我.
Plunker在这里:
https://plnkr.co/edit/57SiCdBZB2OkhdR03VOs?p=preview
(编辑)
注意:
我可以在jQuery中做到:
<script> $(document).ready(function() { get_homepage_data(263,27,'2016-04'); function get_homepage_data(stationIds,crusherIds,date) { var url = "https://myurl../api/getHPData"; var data_to_send = { 'stationId': stationIds,'crusherId': crusherIds,'monthYear': date }; console.log("Value is: " + JSON.stringify(data_to_send)); //change sender name with account holder name // console.log(data_to_send) $.ajax({ url: url,method: 'post',dataType: 'json',//contentType: 'application/json',data: data_to_send,processData: true,// crossDomain: true,beforeSend: function () { },complete: function () {},success: function (result1) { var Result = JSON.parse(result1); var value_data = Result["valueResult"]; var foo = value_data["gyydt"]; console.log("Log of foo is: " + foo); var foo2 = 0; // 10 lac is one million. foo2 = foo / 1000000 + ' million'; console.log(JSON.stringify(value_data["gyydt"]) + " in million is: " + foo2); },error: function (request,error) { return false; } }); } }); // eof Document. Ready </script>
以上脚本的输出是脚本是:
>价值是:{“stationId”:263,“crusherId”:27,“monthYear”:“2016-04”}
> XHR完成加载:POST
“https://myurl../api/getHPData”.
> foo的日志是:26862094
>“26862094”百万是:26.862094万
哪个是完美的.
总结
以上是小编为你收集整理的angularjs – 使用$http POST Content-Type application / x-www-form-urlencoded访问API全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
我们今天的关于使用$ http POST Content-Type应用程序/ x-www-form-urlencoded访问API和http应用于的分享已经告一段落,感谢您的关注,如果您想了解更多关于@JsonProperty无法用于Content-Type:application / x-www-form-urlencoded、@RequestBody接收Content-type为application/x-www-form-urlencoded字符串乱码、angular – 使用x-www-form-urlencoded的HttpClient POST请求、angularjs – 使用$http POST Content-Type application / x-www-form-urlencoded访问API的相关信息,请在本站查询。
本文标签: