在Parse(Javascript)中存储项目

时间:2014-12-30 15:15:58

标签: javascript jquery parse-platform

我想存储两个文本字段(userID和地址),以及用户上传到解析的文件,但是尽管支持,我的尝试仍未成功。我不确定代码中是否存在拼写错误,但它似乎并没有在Parse中记录。

<HTML>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.2.15.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {

  // ***************************************************
  // NOTE: Replace the following your own keys
  // ***************************************************

    Parse.initialize("id", "id");

  function saveDocumentUpload(objParseFile)
  {
     var documentUpload = new Parse.Object("Scan");
     documentUpload.set("Name", "");

     documentUpload.set("DocumentName", objParseFile);
     documentUpload.save(null, 
     {
        success: function(uploadResult) {
          // Execute any logic that should take place after the object is saved.

        },
        error: function(uploadResult, error) {
          // Execute any logic that should take place if the save fails.
          // error is a Parse.Error with an error code and description.
          alert('Failed to create new object, with error code: ' + error.description);
        }
     });
  }

  $('#documentFileUploadButton').bind("click", function (e) {
        var fileUploadControl = $("#documentFileUpload")[0];
        var file = fileUploadControl.files[0];
        var name = file.name; //This does *NOT* need to be a unique name
        var parseFile = new Parse.File(name, file);

        var user_id = $('#user_id').val();

        var address = $('#address').val();


        parseFile.set('UserId', user_id);
        parseFile.set('Address', address);


        parseFile.save().then(
                function () {
                    saveDocumentUpload(parseFile);
                },
                function (error) {
                    alert("error");
                }
        );
    });
});
</script>

<body><form>
    <input type="file" id="documentFileUpload">
    <br/>
    <input type="text" placeholder="UserID" id="user_id">
    <br/>
    <input type="text" placeholder="Address" id="address">
    <br/>
    <input type="submit" id="documentFileUploadButton" value="submit">
</form>
</body>
</HTML>

更新

<HTML>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.2.15.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {

  // ***************************************************
  // NOTE: Replace the following your own keys
  // ***************************************************

    Parse.initialize("ID", "ID");

  function saveDocumentUpload(objParseFile)
  {
     var documentUpload = new Parse.Object("Scan");
     documentUpload.set("Name", "");

     documentUpload.set("DocumentName", objParseFile);
     documentUpload.save(null, 
     {
        success: function(uploadResult) {
          // Execute any logic that should take place after the object is saved.

        },
        error: function(uploadResult, error) {
          // Execute any logic that should take place if the save fails.
          // error is a Parse.Error with an error code and description.
          alert('Failed to create new object, with error code: ' + error.description);
        }
     });
  }

  $('#myForm').bind("submit", function (e) {
    e.preventDefault();
        var fileUploadControl = $("#documentFileUpload")[0];
        var file = fileUploadControl.files[0];
        var name = file.name; //This does *NOT* need to be a unique name
        var parseFile = new Parse.File(name, file);

        var user_id = $('#user_id').val();

        var address = $('#address').val();


        parseFile.set('UserId', user_id);
        parseFile.set('Address', address);


        parseFile.save().then(
                function () {
                    saveDocumentUpload(parseFile);
                },
                function (error) {
                    alert("error");
                }
        );
    });
});
</script>

<body><form id='myForm'>
    <input type="file" id="documentFileUpload">
    <br/>
    <input type="text" placeholder="UserID" id="user_id">
    <br/>
    <input type="text" placeholder="Address" id="address">
    <br/>
    <input type="submit" value="submit">
</form>
</body>
</HTML>

1 个答案:

答案 0 :(得分:0)

首先,您是否正确输入了api密钥?我假设你把它改成了这里的帖子,但如果没有,他们需要你的钥匙。

Parse.initialize("id", "id");

您正在正常发布表单,因此您的javascript未运行。

将您的按钮更改为下方 - 为您的表单提供一些ID - 我为此示例提供了myForm的ID。

而不是:

$('#documentFileUploadButton').bind("click", function (e) {
     //your code to run
});

尝试:

$('#myForm').bind("submit", function (e) {
     e.preventDefault();
     //your code to run
 });