使用Javascript从纯文本API中获取值

Use Javascript to get value from plaintext API

本文关键字:获取 API 文本 Javascript 使用      更新时间:2023-09-26

我更喜欢原生Javascript,但jQuery作为第二个选项也可以

我想使用一个纯文本API,如在这个网站上提供的:

blockchain.info纯文本比特币区块链解析API

然后,我想使用Javascript/jQuery来获取该值,并在代码的其他地方使用它。

我使用jQuery找到了这个例子(首选Native):

$.get("https://blockchain.info/q/addressbalance/1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa", function(response) { alert(response) });

然而,这会发出值警报,我想将该值放入Javascript变量中。

var value;
$.get("https://blockchain.info/address/1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa", function(response) { value=response; });

在ajax调用之后,您将获得变量value 中的值

在中

$.get("https://blockchain.info/address/1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa", function(response) { alert(response) });

response是保持输出的变量。

EDIT:重要的是要知道变量响应只能在大括号中访问。

假设您想调用一个函数并将输出传递给它

$.get("https://blockchain.info/address/1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa", function(response) { my_function(response) });

function my_function( response ) {
   //the variable response holds the text you want.
   //EDIT: response, not my_function. Sorry about that.
}