如何从网页发送参数来解析云功能?

时间:2019-05-09 16:20:26

标签: javascript jquery html node.js parse-platform

我制作了一个Web表单以向数据库添加一些字符串,现在我的add函数运行正常,但是当我单击Submit并将控制台日志发送undefined给我时,我的Web并没有发送参数来运行我不知道什么做的请帮忙,谢谢你

这里是我的功能代码,可以完美运行

Parse.Cloud.define('addSynonym', function (request, response) {
  var SYN = Parse.Object.extend("Synonym");
  var CommonwordFromUser = request.params.common_word;
  var SynonymwordFromUser = request.params.synonym_word;
  console.log(CommonwordFromUser);
  console.log(SynonymwordFromUser);
  if (CommonwordFromUser == null || SynonymwordFromUser == null) {
    response.error("request null values");
  } else {
    var query = new Parse.Query(SYN)
    query.find({
      success: function (synResponse) {
        var synOBJ = new SYN();
        synOBJ.set("common_word", CommonwordFromUser);
        synOBJ.set("synonym_word", SynonymwordFromUser);
        synOBJ.save(null, {
          success: function (success) {
            response.success({
              "common_word": CommonwordFromUser,
              "synonym_word": SynonymwordFromUser
            });
          },
          error: function (error) {
            response.error("save failed : " + error.code);
          }
        });
      }
    })
  }
});

这是我的问题所在的网页

    <form id="contact" class="registerForm">

      <div class="alert alert-success" id="alertbox">
        <strong>Success!</strong> Your Synonym has uploaded.
      </div>

      <div class="text-center">
        <img src="img/icon.png" class="text-center" alt="Responsive image" width="90" height="90">
      </div>
      <h3 class="text-center font-weight-normal">Welcome to synonym Menu</h3>

      <label id="label-tags" for="ask-tags">Common Word :</label>
      <fieldset class="form-group">
        <input id="common_word" name="common_word" placeholder="Please type some word" type="text" tabindex="1" required>
      </fieldset>
      <label id="label-tags" for="ask-tags">Synonym Word :</label>
      <fieldset class="form-group">
        <input id="synonym_word" name="synonym_word" placeholder="Please type some word" type="text" tabindex="2"
          required>
      </fieldset>
      <fieldset class="form-group">
        <button name="submit" type="submit" onclick="submitfrom()" id="contact-submit">Submit</button>
      </fieldset>
      </form>
  </div>

  <!-- jQuery -->
  <script src="js/jquery.js"></script>

  <!-- Bootstrap Core JavaScript -->
  <script src="js/bootstrap.min.js"></script>
  <script src="js/standalone/selectize.min.js"></script>
  <script src="js/bootstrapValidator.min.js"></script>


  <script>
    $('.registerForm').bootstrapValidator({
      message: 'This value is not valid',
      feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
      },
      fields: {
        common_word: {
          validators: {
            notEmpty: {
              message: 'Require !!'
            }
          }
        },
        synonym_word: {
          validators: {
            notEmpty: {
              message: 'Require !!'
            }
          }
        },
      }
    }).on('success.form.bv', function (e) {
      submitfrom();
    });
    $('#alertbox').hide();


    function submitfrom() {
      var common_word = $('#common_word').val();
      var synonym_word = $('#synonym_word').val();


      var obj = [{
        "common_word": common_word,
        "synonym_word": synonym_word,
      }];

      var data = '{"objects":' + JSON.stringify(obj) + '}';
      callParseServerCloudCode("addSynonym", data, function (response) {
        if (response) {
          console.log(response);
          alert(":Ok !");
          location.reload();
        }
      });
      $(this).scrollTop(0);
      $('.registerForm').data('bootstrapValidator').resetForm();
      $('#alertbox').show();
      $('#alertbox').fadeTo(2000, 500).slideUp(500, function () {
        $('#alertbox').hide();
      });
    }


    function callParseServerCloudCode(methodName, requestMsg, responseMsg) {
      var xhr = new XMLHttpRequest();
      xhr.open('POST', 'https://xxxxxxxxxxxx.herokuapp.com/parse/functions/' + methodName, true);
      xhr.setRequestHeader('Content-type', 'application/json');
      xhr.setRequestHeader('X-Parse-Application-Id', 'myAppId');
      xhr.setRequestHeader('X-Parse-Master-Key', 'myMasterKey');

      xhr.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
          var myArr = JSON.parse(this.responseText);
          responseMsg(myArr.result);
        }
      };

      xhr.send(requestMsg);
    }
  </script>

</body>

</html>

1 个答案:

答案 0 :(得分:0)

选项1: 只需更改下面的行,它应该可以工作

var obj = {
  "common_word": common_word,
  "synonym_word": synonym_word,
};

var data = JSON.stringify(obj);

选项2(推荐):

Parse.Cloud.run(
  "addSynonym",
  {
    "common_word": common_word,
    "synonym_word": synonym_word
  }
).then(
  function() {
    console.log('success');
  },
  function (error) {
    console.error(error);
  }
);