如何使用 SparkAR 网络模块获取 JSON

2023-12-10

我想使用 SparkAR 的网络模块从 URL 获取数据 并显示它。

我尝试了 Spark AR 文档中的示例,但效果不大:https://developers.facebook.com/docs/ar-studio/reference/classes/networkingmodule/

不要忘记添加“jsonplaceholder.typicode.com” 首先到 Spark AR 的白名单域。 :)

// Load in the required modules
const Diagnostics = require('Diagnostics');
const Networking = require('Networking');

//==============================================================================
// Create the request
//==============================================================================

// Store the URL we're sending the request to
const url = 'https://jsonplaceholder.typicode.com/posts';

// Create a request object
const request = {

  // The HTTP Method of the request
  // (https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)
  method: 'POST',

  // The HTTP Headers of the request
  // (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers)
  headers: {'Content-type': 'application/json; charset=UTF-8'},

  // The data to send, in string format
  body: JSON.stringify({title: 'Networking Module'})

};

//==============================================================================
// Send the request and log the results
//==============================================================================

// Send the request to the url
Networking.fetch(url, request).then(function(result) {

  // Check the status of the result
  // (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
  if ((result.status >= 200) && (result.status < 300)) {

    // If the request was successful, chain the JSON forward
    return result.json();

  }

  // If the request was not successful, throw an error
  throw new Error('HTTP status code - ' + result.status);

}).then(function(json) {

  // Log the JSON obtained by the successful request
  Diagnostics.log('Successfully sent - ' + json.title);

}).catch(function(error) {

  // Log any errors that may have happened with the request
  Diagnostics.log('Error - ' + error.message);

});

我得到的只是:“>>成功发送 - 网络 模块”

有谁知道如何让 json 内容显示在控制台中,我想存储它并随后在文本对象中使用它。


就我而言,有一个 url,它给我一个 json 格式的随机项目。

URL: https://gabby-airbus.glitch.me/random

结果:{"item":"项目 14"}

在代码中,替换行:

 Diagnostics.log('Successfully sent - ' + json.title);  

有了这个:

// show json data in console
Diagnostics.log(json.item);

// Asign json data to text object
itemText.text = json.item;

第一行显示控制台中的 json 数据。 第二行将 json 数据分配给场景中的文本对象,在本例中为“item Text”文本对象。

完整代码:

const Diagnostics = require('Diagnostics');
const Scene = require('Scene');
const Networking = require('Networking');
const URL = 'https://gabby-airbus.glitch.me/random';
var itemText = Scene.root.find('itemText');

Networking.fetch(URL).then(function(result){
    if( (result.status >=200) && (result.status < 300)){ return result.json(); } 
    else { throw new Error('HTTP Status Code: ' + result.status); }
}).then(function(json){
    // show json data in console
    Diagnostics.log(json.item);

    // Asign json data to text object
    itemText.text = json.item;
}).catch(function(error){ 
    itemText = 'Failed to start'; 
    Diagnostics.log(result.status + error); 
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 SparkAR 网络模块获取 JSON 的相关文章

随机推荐