将字符串存储到Parse(Javascript)

时间:2014-12-31 21:23:22

标签: javascript jquery parse-platform google-drive-api

我想将一个URL作为字符串存储到解析中。

特别是,用户使用Google云端硬盘选择器从Google云端硬盘中选择一个项目,然后生成一个网址。我想获取该URL并将其存储在名为" Scan"。

的类下的解析中。

让我在代码中详细说明:

// A simple callback implementation.
      function pickerCallback(data) {
        var url = 'nothing';
        if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
          var doc = data[google.picker.Response.DOCUMENTS][0];
          url = doc[google.picker.Document.URL];

        }
        var message = 'The following(s) were stored in Parse: ' + url;
        document.getElementById('result').innerHTML = message;

我已经通过

生成了网址
      url = doc[google.picker.Document.URL];

并且网址在网站上通过以下行显示为字符串:

  var message = 'The following(s) were stored in Parse: ' + url;
        document.getElementById('result').innerHTML = message;

我想要的只是将该网址存储到解析中,但是,不确定如何训练此代码并插入我的google驱动器选择器选择代码,如下所示:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>eSnail Scan Upload Part 2</title>

    <script type="text/javascript">

      // The Browser API key obtained from the Google Developers Console.
      var developerKey = 'ID';

      // The Client ID obtained from the Google Developers Console.
      var clientId = 'ID';

      // Scope to use to access user's photos.
      var scope = ['https://www.googleapis.com/auth/photos'];

      var pickerApiLoaded = false;
      var oauthToken;

      // Use the API Loader script to load google.picker and gapi.auth.
      function onApiLoad() {
        gapi.load('auth', {'callback': onAuthApiLoad});
        gapi.load('picker', {'callback': onPickerApiLoad});
      }

      function onAuthApiLoad() {
        window.gapi.auth.authorize(
            {
              'client_id': clientId,
              'scope': scope,
              'immediate': false
            },
            handleAuthResult);
      }

      function onPickerApiLoad() {
        pickerApiLoaded = true;
        createPicker();
      }

      function handleAuthResult(authResult) {
        if (authResult && !authResult.error) {
          oauthToken = authResult.access_token;
          createPicker();
        }
      }

      // Create and render a Picker object for picking user Photos.
      function createPicker() {
        if (pickerApiLoaded && oauthToken) {
          var picker = new google.picker.PickerBuilder().
              enableFeature(google.picker.Feature.MULTISELECT_ENABLED).
              addView(google.picker.ViewId.PDFS).
              setOAuthToken(oauthToken).
              setDeveloperKey(developerKey).
              setCallback(pickerCallback).
              build();
          picker.setVisible(true);
        }
      }

      // A simple callback implementation.
      function pickerCallback(data) {
        var url = 'nothing';
        if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
          var doc = data[google.picker.Response.DOCUMENTS][0];
          url = doc[google.picker.Document.URL];

        }
        var message = 'The following(s) were stored in Parse: ' + url;
        document.getElementById('result').innerHTML = message;




      }


    </script>
  </head>
  <body>
    <div id="result"></div>
    <div id="demo">

    <!-- The Google API Loader script. -->
    <script type="text/javascript" src="https://apis.google.com/js/api.js?onload=onApiLoad"></script>
  </body>
</html>

非常感谢任何帮助。

更新2:下面是一个试图将解析与我的初始代码集成的代码:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>eSnail Scan Upload Part 2</title>

    <script type="text/javascript">

      // The Browser API key obtained from the Google Developers Console.
      var developerKey = 'ID';

      // The Client ID obtained from the Google Developers Console.
      var clientId = 'ID';

      // Scope to use to access user's photos.
      var scope = ['https://www.googleapis.com/auth/photos'];

      var pickerApiLoaded = false;
      var oauthToken;

      // Use the API Loader script to load google.picker and gapi.auth.
      function onApiLoad() {
        gapi.load('auth', {'callback': onAuthApiLoad});
        gapi.load('picker', {'callback': onPickerApiLoad});
      }

      function onAuthApiLoad() {
        window.gapi.auth.authorize(
            {
              'client_id': clientId,
              'scope': scope,
              'immediate': false
            },
            handleAuthResult);
      }

      function onPickerApiLoad() {
        pickerApiLoaded = true;
        createPicker();
      }

      function handleAuthResult(authResult) {
        if (authResult && !authResult.error) {
          oauthToken = authResult.access_token;
          createPicker();
        }
      }

      // Create and render a Picker object for picking user Photos.
      function createPicker() {
        if (pickerApiLoaded && oauthToken) {
          var picker = new google.picker.PickerBuilder().
              enableFeature(google.picker.Feature.MULTISELECT_ENABLED).
              addView(google.picker.ViewId.PDFS).
              setOAuthToken(oauthToken).
              setDeveloperKey(developerKey).
              setCallback(pickerCallback).
              build();
          picker.setVisible(true);
        }
      }

      // A simple callback implementation.
      function pickerCallback(data) {
        var url = 'nothing';
        if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
          var doc = data[google.picker.Response.DOCUMENTS][0];
          url = doc[google.picker.Document.URL];

        }
        var message = 'The following(s) were stored in Parse: ' + url;
        document.getElementById('result').innerHTML = message;

    Parse.initialize("ID", "ID");
           var PDFUpload = new Parse.Object("Scan");
     PDFUpload.set("Name", url);

    PDFUpload.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);
        }
     });

      }


    </script>
  </head>
  <body>
    <div id="result"></div>
    <div id="demo">

    <!-- The Google API Loader script. -->
    <script type="text/javascript" src="https://apis.google.com/js/api.js?onload=onApiLoad"></script>
  </body>
</html>

特别是本节,

// A simple callback implementation.
          function pickerCallback(data) {
            var url = 'nothing';
            if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
              var doc = data[google.picker.Response.DOCUMENTS][0];
              url = doc[google.picker.Document.URL];

            }
            var message = 'The following(s) were stored in Parse: ' + url;
            document.getElementById('result').innerHTML = message;

        Parse.initialize("ID", "ID");
               var PDFUpload = new Parse.Object("Scan");
         PDFUpload.set("Name", url);

        PDFUpload.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);
            }
         });

          }

1 个答案:

答案 0 :(得分:0)

下的

Parse.initialize(&#34; ID&#34;,&#34; ID&#34;); 尝试:

var PDFUploadObject = new Parse.Object.extend("Scan");

var PDFUpload = new PDFUploadObject ();
PDFUpload.set("Name",url);
PDFUpload.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);
            }
         });