我正在使用THIS api 在它周围的范围内搜索邮政编码。例如,当我输入邮政编码 (1234) 时,它会生成自动输出。它为此使用 jquery 自动完成功能。
现在是我的问题。如何将该输出保存在会话中并在该会话中搜索以匹配我在公司数据库中拥有的任何邮政编码?
我的代码如下所示:
我的表格:
<form action="#" method="post" class="pro6pp_range">
<input type="text" class="postcode" placeholder="Postcode (1234)" maxlength="4">
<select class="range">
<option value="5" selected="selected">5 km</option>
<option value="10">10 km</option>
<option value="15">15 km</option>
<option value="20">20 km</option>
<option value="25">25 km</option>
</select>
<br/><br/>
<span class="message"></span>
<div class="output"></div>
<br/>
</form>
我的JavaScript:
<script type="text/javascript">
var pro6pp_auth_key = "MY_AUTH_KEY";
$(document).ready(function() {
$(".pro6pp_range").applyRange({ 'assemble_func': assemble_one });
function assemble_one(result) {
distance_km = Math.round(result.distance / 1000);
return "Afstand tot " + result.nl_fourpp + " is " +
distance_km + " km vanaf uw locatie.<br />";
}
});
</script>
范围.js:
// Create closure to keep namespace clean and hide implementation.
(function($) {
$.fn.applyRange = function(options) {
var instance = this;
function getConfig(field, default_value) {
if (typeof options === 'undefined' ||
typeof options[field] === 'undefined') {
return default_value;
} else {
// Developer chose to specify form field manually.
return options[field];
}
}
instance.postcode = getConfig('postcode', instance.find('.postcode'));
instance.range = getConfig('range', instance.find('.range'));
instance.message = getConfig('message', instance.find('.message'));
instance.spinner = getConfig('spinner', instance.find('.spinner'));
instance.output = getConfig('output', instance.find('.output'));
instance.distance = getConfig('distance', instance.find('.distance'));
instance.lat = getConfig('lat', instance.find('.lat'));
instance.lng = getConfig('lng', instance.find('.lng'));
instance.nl_fourpp = getConfig('nl_fourpp', instance.find('.nl_fourpp'));
instance.assemble_func = getConfig('assemble_func', assemble_one);
instance.postcode.keyup(function() {
range(instance);
});
};
var pro6pp_cache = {};
function pro6pp_cached_get(obj, url, params, callback) {
var key = url + $.param(params);
if (pro6pp_cache.hasOwnProperty(key)) {
if (typeof callback !== 'undefined') {
callback(obj, pro6pp_cache[key]);
}
} else {
$.getJSON(url + "?callback=?", params, function(data) {
pro6pp_cache[key] = data;
if (typeof callback !== 'undefined') {
callback(obj, data);
}
});
}
}
function getApiBaseUrl() {
// Use HTTPS API if website itself is also secure.
// Otherwise, some browsers might complain about insecure content.
if ("https:" === document.location.protocol) {
return 'https://pro6pp.appspot.com/v1';
} else {
return 'http://api.pro6pp.nl/v1';
}
}
function range(obj) {
obj.message.hide().empty();
var postcode = obj.postcode.val();
var range = obj.range.val();
// Trigger on '5408'
var nl_fourpp_regex = /[0-9]{4,4}\s?/;
if (nl_fourpp_regex.test(postcode)) {
var url = getApiBaseUrl() + "/range";
var params = new Object();
params.auth_key = pro6pp_auth_key;
params.per_page = 100;
params.nl_fourpp = postcode;
// User puts in range in kilometers, API uses meters.
range = parseInt(range, 10) * 1000;
params.range = range;
pro6pp_cached_get(obj, url, params, fillin);
}
}
function assemble_one(result) {
distance_km = Math.round(result.distance / 1000);
return "Afstand tot " + result.nl_fourpp + " is " + distance_km + " km<br />";
}
function fillin(obj, json) {
if (json.status == 'ok') {
obj.output.empty();
var postcode = obj.postcode.val();
$.each(json.results, function(i, result) {
if (result.nl_fourpp === postcode) {
return true;
}
obj.output.append(obj.assemble_func(result)).show();
});
if (json.results.length > 1) {
obj.distance.val(json.results[1].distance);
obj.nl_fourpp.val(json.results[1].nl_fourpp);
obj.lat.val(json.results[1].lat);
obj.lng.val(json.results[1].lng);
} else {
translated_message = 'Geen resultaten gevonden';
obj.message.html(translated_message).show();
}
} else {
var translated_message = json.error.message;
if (json.error.message == 'nl_fourpp niet gevonden') {
translated_message = 'Onbekende postcode';
} else if (json.error.message == 'Invalid nl_fourpp format') {
translated_message = 'Ongeldig postcode formaat';
} else if (json.error.message == 'invalid target_nl_fourpps format') {
translated_message = 'Ongeldig target_nl_fourpps formaat';
}
obj.message.html(translated_message).show();
}
}
})(jQuery);
我希望有人知道如何将请求的输出保存到会话/cookie。