简体   繁体   English

Taxee API(http://www.taxee.io/)-JavaScript-如何:POST请求和传递参数

[英]Taxee API (http://www.taxee.io/) - JavaScript - How To: POST Request and Pass Parameters

Thanks for helping out with my question. 感谢您为我的问题提供帮助。 I've been trying to get the federal, state, and FICA tax from a POST Request on this Taxee API ( https://market.mashape.com/stylinandy/taxee ), but haven't been able to get it working. 我一直在尝试从此Taxee API( https://market.mashape.com/stylinandy/taxee )上的POST请求中获取联邦税,州税和FICA税,但一直无法正常工作。 I was able to access certain data (simply to figure out how this API works) using one of the two GET requests available for this API: 我可以使用可用于此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();

But the data I really need is in the POST request. 但是我真正需要的数据在POST请求中。 I would like to know how to access a POST request for this, and more specifically, pass the parameters as noted on the link above. 我想知道如何访问POST请求,更具体地说,传递上面链接中指出的参数。 Thanks for any help you can provide, it's always greatly appreciated. 多谢您提供的任何协助,我们始终感激不尽。

Figured out how to do it actually. 弄清楚实际如何做。 The problem was the way that parameters are passed differently in POST requests. 问题在于在POST请求中以不同方式传递参数的方式。 Hopefully this helps someone else out there: 希望这可以帮助其他人:

  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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM