0
我們有一個大學校園的數據庫,我試圖爲每個州創建鏈接。在我們的網站上,它將是一個被點擊的SVG地圖,但爲了演示目的,它只是一個文本鏈接。我無法弄清楚如何連線。將變量傳遞給jQuery jsonp
我已經在一些json結果中硬編碼,以確保代碼的一部分工作,但我不知道如何做到這一點,當頁面上的鏈接被點擊時,它將狀態傳遞到json請求。
這裏爲演示HTML
<a href="http://ml.uscm.org/ministries.json?state=WY&active=true&callback=Wyoming">Wyoming</a>
<a href="http://ml.uscm.org/ministries.json?state=WY&active=true&callback=Florida">Florida</a>
<span class="results"></span>
這裏是JS
$(document).ready(function() {
var jsonp = '[{"city":"Casper","enrollment":"4426","name":"Casper College","state":"WY","tuition":2232,"url":"http://www.campusventures.org","zip":"82601-2458","id":3928},{"city":"Riverton","enrollment":"2316","name":"Central Wyoming College","state":"WY","tuition":2472,"url":"","zip":"82501-1520","id":818},{"city":"Torrington","enrollment":"1965","name":"Eastern Wyoming College","state":"WY","tuition":2376,"url":"http://www.campusventures.org","zip":"82240-1699","id":819},{"city":"Cheyenne","enrollment":"5302","name":"Laramie County Community College","state":"WY","tuition":2640,"url":"","zip":"82007-3299","id":820},{"city":"Sheridan","enrollment":"4296","name":"Northern Wyoming Community College District","state":"WY","tuition":2398,"url":"","zip":"82801-1500","id":5825},{"city":"Laramie","enrollment":"13922","name":"University of Wyoming","state":"WY","tuition":4278,"url":"http://www.cvuw.com","zip":"82071-3434","id":4073},{"city":"Rock Springs","enrollment":"4013","name":"Western Wyoming Community College","state":"WY","tuition":1145,"url":"","zip":"82902-0428","id":823}]';
var results = '';
var obj = $.parseJSON(jsonp);
$.each(obj, function() {
results += "<h3><a href='http://ml.uscm.org/ministries/" + this['id'] + ".json" + "'>" + this['name'] + "</a></h3>";
results += this['city'] + "<br/>";
$('span.results').html(results);
});
});
這是我到目前爲止的jsfiddle example。
我敢肯定,在我的代碼的變種JSONP需要改變基本URL並獲得通過單擊的狀態。如果您現在單擊鏈接,它會簡單地加載未格式化的結果,我需要它們以我的硬編碼演示數據的方式顯示在頁面上。對不起,如果我沒有解釋得很好。 –