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

标签 json https request spark-ar-studio

我想使用 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 格式的随机项目。

网址: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 数据分配给场景中的文本对象,在本例中为“itemText”文本对象。

完整代码:

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

关于json - 如何使用 SparkAR 网络模块获取 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56960033/

相关文章:

http - 如何使第三方 URL 安全

php - getimagesize() 无法打开流 : HTTP request failed! 错误

javascript - Nodejs request.get 响应 "Web Page Blocked!"

c# - 在 asp.net 中通过请求生命周期保存值(value)

https - 如何为独立的 Wiremock 启用 HTTPS

java - Jackson 总是序列化完整对象或总是只序列化 id

javascript - 读取 Json 变量

php - Slim 框架无法使用 protected 变量编码为 json

java - 如何使用 Gson、Retrofit、Active Android 将嵌套 JSON 对象反序列化为字符串

java - 在给定的 java 代码中禁用 SSL 连接