2014-02-13 20 views
0

這是我想達到的目標:使在Javascript API調用 - 在每一個新的呼叫單擊

我網頁上的元素,讓我們稱之爲是風暴
每次在用戶點擊風暴鏈路,我想以下:

  • 執行的API調用,其參數是預定義的但與風暴字符串作爲其中之一
  • 將結果(API生成JSON文件)存儲在某處
  • 用一種或另一種方法解析結果。

我沒有問題解析JSON返回,但我想知道如何執行前兩個步驟。 注:我比jQuery更多地使用JS,但我不是納粹對此的。

非常感謝您的幫助。

編輯:謝謝@ema
我有一個XHR模型,附在這裏。 你能幫我確定我必須在哪裏添加API網址(從我瞭解的內容,第一個問號之前的內容),以及如何添加預定義的參數和自定義參數(字符串,包含風暴爲例)?

再次感謝

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

'$ .post'或'XMLHttpRequest'應該可以做到。確切的電話和說明取決於您正在處理的API。 –

回答

2

您可以使用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"); 

HTH

+0

謝謝@ema。剛剛編輯更精確的問題。 – basbabybel

0

讓我看看,如果我的理解..

要調用的API來自JQuery非常簡單,你可以使用$ .ajax(最完整)或$ .post(最簡單)

都調用上的click事件,你可以用。就緒

$(document.ready(function(){ 
    $('.mybuttons').off('click');//to only hit once if you have Postbacks 
    $('.mybuttons').on('click', myapicall); 
}); 

例如綁定的$(document)以$阿賈克斯:

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

例如用$。員額

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

希望我能幫助你,對不起英文不好,;-)