Retrieve data from an API using the NativeScript Http module and store the response in a variable

Is there a way to send an HTTP GET or POST request to a server in order to assign the return value to a variable? I found this example in the NativeScript docs:

httpModule.getString("https://httpbin.org/get").then((r) => {
    viewModel.set("getStringResult", r);
}, (e) => {
});

I am confused about what 'r' contains and what 'viewModel' represents. In jQuery, capturing an AJAX request is straightforward like this:

let returnValue;
$.post(url, function(data) { 
    returnValue = data;
});

Answer №1

When using the method httpModule.getString(...), it will return the response body as a string. The ViewModel typically represents the Observable instance of your View or Page.

Therefore, the variable r in this context holds the string that is returned by the endpoint, and you can simply assign it to any other variable.

httpModule.getString("https://httpbin.org/get").then((d) => {
   returnVal = d;
}, (e) => {
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Deciphering JSON information extracted from a document

I am currently working on a Node JS project where I need to read a file containing an array of JSON objects and display it in a table. My goal is to parse the JSON data from the array. Below is a sample of the JSON data: [{"name":"Ken", "Age":"25"},{"name" ...

What could be the reason for my SQL query functioning correctly in my editor, but not in my JavaScript function?

When I run my SQL statement in my SQL editor, it works perfectly fine. However, when I try to use it in my JavaScript function, I get an error saying there is an invalid column for my client ID. function getFeedposts(data) { var limit = data.dollarLimit; ...

Struggling to Play Sound on Vue Template

I attempted to implement the audio tag using a variable, but it was not functioning properly. Any assistance would be greatly appreciated. The following code is within the script tag: <script setup> import { ref } from 'vue' import data fr ...

Is it possible to use a single function to both set the state and return JSX?

I'm currently grappling with creating a function that, when called, will update the state to an object {item: 'whatever the user types', list: [...list, todo]}. The challenge I'm facing is figuring out how to update the state and return ...

Using the back button in the browser can undo any CSS modifications made by JavaScript

I'm currently working on a form that changes the displayed select lists based on the selection of a previous list. HTML <select name="college" id="college" onchange="show_majors()"> <!-- <option value="-">-</option& ...

tips for sending the chosen product to axios

How can I send the selected item from the dropdown menu to Axios in order to retrieve data? I need to pass the item itself, not just the ID, to the API. <label>City</label> <select @change="getArea()" v-model="key"> <option :valu ...

Unable to write or upload error in a Node Express application

My GET and POST APIs are functioning properly, however, my app.put is not working as expected. https://i.sstatic.net/Oc0QT.png Upon sending a PUT request to localhost:3001/contacts/1 using Postman, I am unable to see the console.log output: https://i.ss ...

Is there a way to smoothly navigate back to the top of the page after the fragment identifier causes the page to scroll down?

I do not want to disable the functionality of the fragment identifier. My goal is for the page to automatically scroll back to the top after navigating to a specific section. This inquiry pertains to utilizing jQuery's UI tabs. It is crucial to have ...

When the time comes, ReactDOM will render your element into the designated container,

What does the [,callback] parameter represent in the ReactDOM.render(element, container) method? ...

AngularJS anticipates the completion of a lone asynchronous request

Attempting to assign data to the scope after an asynchronous call. There is an array named companies in the factory. factory.getByCategoryId = function (id) { $http.get('http://localhost/campaign?access-token=12345&id=2').then( func ...

Is there a way to determine if an element has been scrolled past?

I am currently working on a script to detect when a specific element comes into view while scrolling. const targetElement = document.getElementById('sidebar'); window.addEventListener('scroll', () => { if (window.scrollY > tar ...

If the left margin is equal to a percentage

I am trying to create a unique movement for my div - it should move left five times and then come back. To achieve this, I have the following script: Javascrpit : $(document).ready(function() { if(document.getElementById('twitter').style.marg ...

Using Jquery to attach an event listener to an iframe

**I am currently utilizing a jQuery textselect plugin to trigger alerts based on the selected text anywhere on the page, and it is functioning well with code like this: $(document).ready(function() { $(document).bind('textselect', function( ...

"Create a dynamic one-page website using Django framework featuring a modern lateral navigation

My current setup is similar to the one found here, but with my own modifications. https://i.sstatic.net/0aake.png This layout represents my 'home' view. I want to maintain the two sidebars while only refreshing the main content section. So, my ...

Managing State Changes with Redux

Reducers in Redux are primarily designed to be pure functions that take the previous state and an action as arguments, and return a new state object without mutating the previous state. However, it is still possible for developers to directly mutate the st ...

How to Update Mysql Database Records with PHP and Ajax

I need to create a button that, when clicked, will increase a fixed number in the database. The database is named var_stat and contains columns id and value. Currently, there is only one row with id = var and value = 35. When the button is clicked, I want ...

Retrieve information through an AJAX or JavaScript request and use it to fill the selected plugin's multiple selection feature

I have searched everywhere for a solution to this problem, but I haven't been able to find any helpful resources. I apologize in advance if this question has been asked before. What I need to do is to display the data from a JSON object that is fetch ...

Exploring the world of JSON manipulation in JavaScript

I've been experimenting with JSON recently and came across something I don't quite understand. Here is an example of some code I used: var str = "{'name':'vvv'}"; var cjson = eval ("(" + str + ")"); alert( ...

Guide to making control bar fade out when video is paused on video.js

Is there a way for the control bar to automatically disappear when the video is paused? Thank you in advance! ...

Overlap and cover additional elements within a DIV

I am looking to create a versatile function that can effortlessly overlay various elements such as selects, textfields, inputs, divs, tables, and more with a partially transparent div matching their exact height and width. I have managed to obtain the pos ...