在Javascript中进行API调用-每次单击时进行新调用

在Javascript中进行API调用-每次单击时进行新调用,javascript,jquery,json,api,Javascript,Jquery,Json,Api,以下是我想要实现的目标: 我在网页上有一个元素,让我们称之为风暴 每次用户单击storm链接时,我都希望看到以下内容: 执行API调用,这些参数是预定义的,但风暴字符串是其中之一 将结果(API生成JSON文件)存储在某个地方 用一种或另一种方法解析结果 解析返回的JSON没有问题,但我想知道如何执行前两个步骤。 注意:我使用JS比使用jQuery更多,但我在这方面不是纳粹分子 非常感谢你的帮助 编辑:谢谢@ema 我有一个XHR模型,附在下面。 您能帮我确定在哪里添加API url(据我所

以下是我想要实现的目标:

我在网页上有一个元素,让我们称之为风暴
每次用户单击storm链接时,我都希望看到以下内容:

  • 执行API调用,这些参数是预定义的,但风暴字符串是其中之一
  • 将结果(API生成JSON文件)存储在某个地方
  • 用一种或另一种方法解析结果
解析返回的JSON没有问题,但我想知道如何执行前两个步骤。 注意:我使用JS比使用jQuery更多,但我在这方面不是纳粹分子

非常感谢你的帮助

编辑:谢谢@ema
我有一个XHR模型,附在下面。 您能帮我确定在哪里添加API url(据我所知,第一个问号之前是什么),以及如何向其中添加预定义参数和自定义参数(例如,包含storm的字符串)

再次感谢

function XHR(url, method, data, onsuccess, onfailure, onprogress, onload, onabort) {
var request = new XMLHttpRequest();
// Ten seconds is ought to be enough for anybody.
var xhrtimeout = setTimeout(onfailure, 10000);
request.addEventListener("progress", onprogress, false);
request.addEventListener("load", onprogress, false);
request.addEventListener("error", onfailure, false);
request.addEventListener("abort", onabort, false);
request.addEventListener("readystatechange", function (e) {
  if (request.readyState == 4) {
    if (request.status == 200) {
      clearTimeout(xhrtimeout);
      onsuccess(request.responseText);
   } else {
    onfailure(e);
   }
 }
});

 request.open(method, url, true);
 request.send(data);
}


function getJSONAndParse(url, allDone) {
  XHR(url, "GET", null, function(data) {
    allDone(JSON.parse(data));
  }, function() {
    alert("error");
 });
}

getJSONAndParse("http://lalala.com/json", function(parsedJSON) {
  alert(parseJSON[0].name);
  console.log(parseJSON);
});

您可以使用XMLHttpRequest,如下所示:

var r = new XMLHttpRequest();
r.open("POST", "api/url", true);
r.onreadystatechange = function () {
  var json = r.responseText;
  // parse your json here
};
r.send("storm=value_of_storm&another_param=value_of_whatever");

让我看看我是否明白了

从JQuery调用API非常简单,可以使用$.ajax(最完整)或$.post(最简单)

两者都可以调用click事件,您可以在$(document)中绑定该事件

$(document.ready(function(){
    $('.mybuttons').off('click');//to only hit once if you have Postbacks
    $('.mybuttons').on('click', myapicall);
});
美元.ajax的示例:

function myapicall(){
$.ajax({
    beforeSend: function () {
        // a 'Loading' 
    },
    type: 'POST',
    url: 'URL-OF-API',
    contentType: 'application/json; charset=utf-8',
    data: { stormvalue: 'the sorm value you want to send to API'},
    dataType: 'json',

    success: function (json) {
        try {
            json = JSON.parse(json.d);
            // now you have a json object to play with, and if its a string, then you can """save""" using eg. input hidden

        } catch (ex) {
            alert(ex);//show the error parsing json
        }
    },
    error: function (xhr, ajaxOptions, thrownError) {

        var r = jQuery.parseJSON(xhr.responseText);
        alert(r); // show the error of callback
    },
    complete: function (json) {
        alert('sucess!');
    }
});
}
以$.post为例

function myapicall(){
$.post('URL-OF-API', { stormvalue: 'the sorm value you want to send to API'},
    function (json) {
        try {
            json = JSON.parse(json.d);
            // now you have a json object to play with, and if its a string, then you can "save" using eg. input hidden

        } catch (ex) {
            alert(ex);//show the error parsing json
        }
    }
);
}

希望我能帮助你,抱歉英语不好,;-)

$.post
XMLHttpRequest
应该可以做到这一点。确切的调用和说明取决于您处理的API。谢谢@ema。只是更精确地编辑了这个问题。