I am using UnityWebRequest to get data from an api that I created. It uses javascript/jQuery to get data from a server and I used ajax to stop the page from loading before the api gets the data. I need to wait for the page to load before I get the HTML from the api. Please Help!
Javascript/JQuery:
var playlistName = document.location.href.split("#")[1]
$.ajax({
url: "https://cors-anywhere.herokuapp.com/https://gaana.com/playlist/" + playlistName,
success: function(html) {
var htmlPrev = document.outerHTML;
document.write(html)
var elements = document.getElementsByClassName("draggable ");
var data = {name: document.getElementsByClassName("_t1")[0].children[0].textContent, image: document.getElementsByClassName("_at_g1")[0].children[0].src, data: []}
for (var element of elements) {
var currentJSON = JSON.parse(element.getElementsByTagName("span")[0].textContent);
var title = currentJSON.title;
var img = currentJSON.atw;
data.data.push({title, img});
}
console.log(data)
document.outerHTML = htmlPrev;
document.body.textContent = JSON.stringify(data)
},
async:false,
cache:false
});
Unity:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
using TMPro;
public class CallApi : MonoBehaviour
{
public TMP_InputField playlistId;
public static string playlistData;
public void CallPub()
{
StartCoroutine(Call());
}
IEnumerator Call()
{
using (UnityWebRequest request = UnityWebRequest.Get("https://gaana-api--programmer101.repl.co/#" + playlistId.text))
{
yield return request.SendWebRequest();
if (request.isNetworkError)
{
Debug.Log(request.error);
} else
{
CallApi.playlistData = request.downloadHandler.text;
SceneManager.LoadScene("LoadingScene");
}
}
}
}
↧