我試圖訪問此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);
})
但我總是得到這個:
Object {data: "{"result":"false"}", status: 200, config: Object, statusText: "OK", headers: function}
OR
{"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":" 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'},
它提供:
Object {data: null, status: -1, config: Object, statusText: "",headers: function}
OR
{"data":null,"status":-1,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"headers":{"Content-Type":"application/json; charset=UTF-8","Accept":"application/json, text/plain, /"},"url":" https://myurl../api/getHPData ","data":{"stationId":263,"crusherId":27,"monthYear":"2016-4"}},"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>
以上腳本的輸出是script
是:
- 值是:{ 「的stationID」:263, 「crusherId」:27, 「monthYear」: 「2016-04」}
- XHR加載完成:POST 「https://myurl../api/getHPData」。
- 登錄FOO的是:26862094
- 「26862094」 百萬是:26862094
這是完美的。 :)
你好。謝謝您的回答。它正在工作。 Whao。 :)但是你能告訴我:'我怎麼知道我將使用:application/x-www-form-urlencoded',我怎麼知道'string'是雙重序列化的。你能否添加一些更多的細節?以便將來也能幫助我。謝謝你 – fWd82
我將需要:'gytotal'的價值在裏面:'valueResult',你可以給我一些想法怎麼做嗎? – fWd82
'parseFloat(response.data.valueResult.gytotal)' – georgeawg