Taxee API (http://www.taxee.io/) - JavaScript - 如何:POST 请求和传递参数
Taxee API (http://www.taxee.io/) - JavaScript - How To: POST Request and Pass Parameters
感谢您帮我解答问题。我一直在尝试从关于此 Taxee API (https://market.mashape.com/stylinandy/taxee) 的 POST 请求中获取联邦、州和 FICA 税,但未能成功。我能够使用可用于此 API 的两个 GET 请求之一访问某些数据(只是想弄清楚 API 是如何工作的):
var state = 'CA';
var year = 2014;
var url = ' https://taxee.io/api/v1/state/'+year+'/'+state;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", url, true);
xmlhttp.onload = function() {
var result = JSON.parse(this.responseText);
console.log(result.single.income_tax_brackets);
xmlhttp.abort();
}
xmlhttp.send();
但我真正需要的数据在POST请求中。我想知道如何为此访问 POST 请求,更具体地说,传递上面 link 中提到的参数。感谢您提供的任何帮助,我们将不胜感激。
弄清楚了实际操作方法。问题是在 POST 请求中传递参数的方式不同。希望这可以帮助其他人:
var url = 'https://taxee.io/api/v1/calculate/2014';
var params = "filing_status=married&pay_rate=100000&state=CA";
var http = new XMLHttpRequest();
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);
感谢您帮我解答问题。我一直在尝试从关于此 Taxee API (https://market.mashape.com/stylinandy/taxee) 的 POST 请求中获取联邦、州和 FICA 税,但未能成功。我能够使用可用于此 API 的两个 GET 请求之一访问某些数据(只是想弄清楚 API 是如何工作的):
var state = 'CA';
var year = 2014;
var url = ' https://taxee.io/api/v1/state/'+year+'/'+state;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", url, true);
xmlhttp.onload = function() {
var result = JSON.parse(this.responseText);
console.log(result.single.income_tax_brackets);
xmlhttp.abort();
}
xmlhttp.send();
但我真正需要的数据在POST请求中。我想知道如何为此访问 POST 请求,更具体地说,传递上面 link 中提到的参数。感谢您提供的任何帮助,我们将不胜感激。
弄清楚了实际操作方法。问题是在 POST 请求中传递参数的方式不同。希望这可以帮助其他人:
var url = 'https://taxee.io/api/v1/calculate/2014';
var params = "filing_status=married&pay_rate=100000&state=CA";
var http = new XMLHttpRequest();
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);