将变量传递给jQuery jsonp

时间:2013-04-17 11:19:55

标签: jquery jsonp

我们有一个大学校园数据库,我正在尝试为每个州创建链接。在我们的网站上,它将是一个点击的SVG地图,但出于演示目的,它只是一个文本链接。我无法弄清楚如何连接它。

我已经在一些json结果中进行了硬编码以确保代码的一部分有效,但我不知道如何在单击页面上的链接时将其传递给json请求。 / p>

这是演示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

1 个答案:

答案 0 :(得分:0)

很抱歉,如果我弄错了,但你的意思是简单地将状态作为变量传递给JSON请求,就像这样吗?

?state=" + this['state'] + "

http://jsfiddle.net/richcoy/nfNMr/1/