为什么API无法正常工作?

时间:2016-12-08 00:29:21

标签: javascript html json api access-token

为什么提交按钮没有按原样输出结果?我似乎无法注意到代码可能出现的问题。基本上它是来自Mashape(https://market.mashape.com/nviror/bmi-calculator)的BMI API,我无法让它工作。我是JavaScript和API的新手,所以我无法解决这个问题。 这是javascript的javascript和表单部分:

// TODO: Use a function closure and release global $
$.noConflict();
(function($){
  $(document).ready(function() {
    $('#bmi-form').on('submit', function(event) {
      //saving inputs
      var weight = $('#bmi-weight').val();
      var height = $('#bmi-height').val();
      
      $.get(
          'https://bmi.p.mashape.com/WyFUMDOkdrmshARoxfXDWLZmMeccp180tJEjsnCz3MCFuXJdEo' + weight + height,
          function(data,json){
            $('#bmi').append(
            '<p>Your BMI is: '+data.bmi.value+'</p>'
          );
          $('#bmi').append(
            '<p>Your status is: '+data.bmi.status+'</p>'
          );
          $('#bmi').append(
           '<p>Your risk is: '+data.bmi.risk+'</p>'
          );
      });  
        var string= 'Your Bmi: ' +value+ '. Your status:' +status+ '. Your risk:' +risk + '.';
        $('#results').text(string);

    });
  event.preventDefault();
  });
});(jQuery);
<section id="bmi">
        <form id="bmi-form" name="bmi-form" method="get" action="#null">
          <label for="bmi-weight">Type in your weight:</label>
          <input type="text" id="bmi-weight" name="bmi-weight" />
          <label for="bmi-height">Type in your height:</label>
          <input type="text" id="bmi-height" type="text" name="bmi-height" />
          <input type="submit" id="bmi-submit" name="bmi-submit" value="Check BMI" />
        </form>
        <p id="results"></p>
      </section>

1 个答案:

答案 0 :(得分:0)

做这样的事情:

注意:遗憾的是我没有测试下面的代码

$.ajax(
    {
        method: "POST",
        url: "https://bmi.p.mashape.com/",
        headers: {
            "X-Mashape-Key": <enter your API key here>,
            "Content-Type": "application/json",
            "Accept": "application/json"
        },
        data: '{"weight":{"value":"85.00","unit":"kg"},"height":{"value":"170.00","unit":"cm"},"sex":"m","age":"24","waist":"34.00","hip":"40.00"}'
    }
).done(function(){
    console.log("Done");
})