I need to show multiple wfs features into map. The requested url is http://localhost/pdan/map.php?id=150 This is what I have done:
var id = <?php echo $_REQUEST['id']; ?>;
function getVector(obj) {
var url = "";
var geojsonFormat = new ol.format.GeoJSON();
var vectorSource = new ol.source.Vector({
loader: function(extent, resolution, projection) {
var url = 'http:192.168.0.101:8082/geoserver/pdan/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=pdan:' + obj.data_set_name + '&' +
'outputFormat=text/javascript&format_options=callback:loadFeatures' +
'&srsname=EPSG:3857&bbox=' + extent.join(',') + ',EPSG:3857';
$.ajax({url: url, dataType: 'jsonp', jsonp: false, cache:false});
},
strategy: ol.loadingstrategy.bbox
});
window.loadFeatures = function(response) {
vectorSource.addFeatures(geojsonFormat.readFeatures(response));
};
return new ol.layer.Vector({
source: vectorSource,
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'rgba(0, 0, 255, 1.0)',
width: 2
})
})
});
}
function getObject(id) {
var result;
$.ajax({
url: "dbQueries.php",
async: false,
cache: false,
dataType: "JSON",
data: {'query': 110, 'id': id},
success: function (data) {
result = data;
}
});
return result;
}
var vector = [];
var rowObject = getObject(id);
var size = Object.size(rowObject);
//Prepare vector Layer
if (size != 0) {
for (var i = 0; i < size; i++) {
vector[i] = getVector(rowObject[i]);
}
}
//Now Initialise map
var map = createMap();
var graticule = getGraticule();
graticule.setMap(map);
//Add vector to the map
if (size != 0) {
for (var i=0; i<size; i++) {
map.addLayer(vector[i]);
}
}
Everything seems okey to me but the url is changed and in the console I get error as:
GET
http://localhost/pdan/192.168.0.101:8082/geoserver/pdan/ows [HTTP/1.1 403 Forbidden 3ms]
What am I missing here?
The problem was with the url I requested. It should be
var url = 'http://192.168.0.101:8082/geoserver/pdan/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=pdan:' + obj.data_set_name + '&' +
'outputFormat=text/javascript&format_options=callback:loadFeatures' +
'&srsname=EPSG:3857&bbox=' + extent.join(',') + ',EPSG:3857';
instead of
var url = 'http:192.168.0.101:8082/geoserver/pdan/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=pdan:' + obj.data_set_name + '&' +
'outputFormat=text/javascript&format_options=callback:loadFeatures' +
'&srsname=EPSG:3857&bbox=' + extent.join(',') + ',EPSG:3857';
Related
I am getting the list of root folders of OneDrive and sub folders using AJAX post using the following code.
$(document).ready(function(){
$(".driveFolders li").click(function(){
var folderId = $(this).attr( "id" );
var objlst= $(this);
var request;
request = $.ajax({
url: url,
method: "POST",
data: {'id': folderId},
ContentType: 'application/json'
});
request.done(function( response ) {
var result = jQuery.parseJSON(response);
var html="";
for(var i = 0, l = result.length; i < l; i++){
var subFolderID = result[i].id;
html += "<li id="+subFolderID+"'>" + result[i].name + "</li>";
}
$("#childern").html(html);
});
})
})
It returns the list of all files and folders. Now, I got the AJAX to generate data of li elements and I am sending a AJAX request again using the following code:
$(document).on("click", "#childern li", function () {
var subFolderID = $(this).attr("id");
var objlst = $(this);
var request;
request = $.ajax({
context: this,
url: url,
method: "POST",
data: { 'id': subFolderID },
ContentType: 'application/json'
});
request.done(function (response) {
var result = jQuery.parseJSON(response);
var html = "";
for (var j = 0, l = result.length; j < l; j++) {
}
$("#childern").html(html);
});
});
It returns an error
folders does not exist.
While sending the folder id manually, I receive all files. Here is my controller code:
public function GetFolderContent(){
$this->load->library('STOneDrive');
if (!$this->stonedrive->is_Connected) {
$refreshToken = "";
$this->stonedrive->Reconnect($refreshToken);
}
$contents = $this->stonedrive->LoadRootFolders(urldecode($this->input->post('id')));
$result = array();
foreach($contents as $content){
array_push($result,array('id' => $content->getId(),'name'=>$content->getName(),'size'=> ''));
}
echo json_encode($result);
}
There is mistake to append id to li tag:
change
html += "<li id="+subFolderID+"'>" + result[i].name + "</li>";
to following
html += "<li id='"+subFolderID+"'>" + result[i].name + "</li>";
In ajax code method replace with type and content-type with dataType.
I've got a bit of code to divide a string of words in parts and do a request (foreach part) to php to get results and place them in element. All works fine when I use async : false BUT I would like it to be be async : true. The problem I have with async : true is that the results are "random" because request doesn't goes sequential... I've read and searched how to solve this but couldn't find it...
Here's my code:
$(function() {
$("#check").on( "click", function() {
$("#results").empty();
var text = $("#original").val();
var totalWords = text.split(' ').length;
var start = 0;
var numberResults = 10;
var divide = totalWords / numberResults;
var numberOfTimes = Math.floor(divide) + 1;
var test = 0;
for(var index = 0; test < totalWords; index+=10){
var part = text.split(/\s+/).slice((test),(test + 10)).join(" ");
$.ajax({
url: "requests.php",
async : true,
cache: false,
type: "POST",
data: {words : part},
success: function(html){
$("#results").append(html);
$("#check").hide();
}
});
console.log(test);
test = test + 10;
$(".progress-bar").attr("aria-valuenow", test);
$(".progress-bar").css({"min-width" : test + "em"});
$(".progress-bar").css({"width" : test + "%"});
$(".progress-bar").text(test + "%");
console.log(part);
}
});
});
Asynchronous Execution cannot guaranty order of execution. Hence the name. If you want synchronous as you mentioned use async: false.
jQuery docs
Alternatively, use a solution to rearrange the data or use some additional parameter to position the data in your required order if you want to reap the advantages of async execution such as speed.
Example of one such implementation logic is:
$(function() {
$("#check").on("click", function() {
$("#results").empty();
var text = $("#original").val();
var totalWords = text.split(' ').length;
var start = 0;
var numberResults = 10;
var divide = totalWords / numberResults;
var numberOfTimes = Math.floor(divide) + 1;
var test = 0;
function action_task(index, data) {
$.ajax({
url: "requests.php",
async: true,
cache: false,
type: "POST",
data: data,
success: function(html) {
//add data at reserved spot
$("#id_" + index).html(html);
$("#check").hide();
}
});
}
for (var index = 0; test < totalWords; index += 10) {
var part = text.split(/\s+/).slice((test), (test + 10)).join(" ");
//reserving a spot
$("#results").append("<span id='id_" + index + "'></span>");
action_task(index, {
words: part
});
console.log(test);
test = test + 10;
$(".progress-bar").attr("aria-valuenow", test);
$(".progress-bar").css({
"min-width": test + "em"
});
$(".progress-bar").css({
"width": test + "%"
});
$(".progress-bar").text(test + "%");
console.log(part);
}
});
});
i am new to Jquery/Ajax and i am trying to have the source url for the json change based on the url parameters i setup, i have working version in PHP, but i don't know how to write it in JQuery
This is my PHP Code (what i am currently using
$id = urlencode($_GET['id']);
$page = urlencode($_GET['results']);
$url = "https://gdata.youtube.com/feeds/api/playlists/$id?alt=jsonc&v=2&max-results=25&&start-index={$results}";
This code grabs the id and includes it to alter the url of the source file used in the script
so how would i make this code act in the same way?
$(document).ready(function() {
startindex = 1;
loadmore = 20;
addMore(startindex, loadmore);
$('#addmore').on('click',function(e) {
e.preventDefault();
addMore($('#list li').length, 20);
});
});
function addMore(startindex,loadmore) {
src = "https://gdata.youtube.com/feeds/api/playlists/ID_WOULD_GO_HERE?alt=json&max-results=" + loadmore + "&start-index=" + startindex;
$.ajax({
dataType: "jsonp",
url: src,
success: function(data, textStatus, jqXHR) {
if (data.feed && data.feed.entry) {
var $list = $('#list');
$.each(data.feed.entry, function(i, e) {
$list.append('<li class="video"><img src="'+ e.media$group.media$thumbnail[0].url +'" width="250"></img><br>' + e.title.$t + '<P>' + e.author[0].name.$t + ' | '+ e.yt$statistics.viewCount +' Views</span></li>');
});
}
}
});
}
Please help, Thanks!
Please try this:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="list"></div>
<script>
$(document).ready(function() {
startindex = 1;
loadmore = 20;
id = urlVar("id");
if (id!="") {
addMore(id, startindex, loadmore);
}
$('#addmore').on('click',function(e) {
e.preventDefault();
addMore(id, $('#list li').size(), 20);
});
});
function urlVar(varName) {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars[varName]?vars[varName]:"";
}
function addMore(id, startindex,loadmore) {
src = "https://gdata.youtube.com/feeds/api/playlists/"+ id +"?alt=json&max-results=" + loadmore + "&start-index=" + startindex;
$.ajax({
dataType: "jsonp",
url: src,
success: function(data, textStatus, jqXHR) {
console.log(data);
if (data.feed && data.feed.entry) {
var $list = $('#list');
$.each(data.feed.entry, function(i, e) {
$list.append('<li class="video"><img src="'+ e.media$group.media$thumbnail[0].url +'" width="250"></img><br>' + e.title.$t + '<P>' + e.author[0].name.$t + ' | '+ e.yt$statistics.viewCount +' Views</span></li>');
});
}
}
});
}
</script>
To test: this_script.php?id=RD029cW4vF6U2Dc
Potentially you could also get PHP to put the variable into the URL before hand.
Example:
src = "https://gdata.youtube.com/feeds/api/playlists/<?php echo $_GET['id'];?>?alt=json&max-results=" + loadmore + "&start-index=" + startindex;
I downloaded the script from here
http://www.webresourcesdepot.com/fly-to-basket-effect-with-jquery/
its good but some bugs like
Double Click => 3 Items add to basket
Tripple Click => 7 Items add to basket
i was trying to fix it but still cant get something .. then i see this link Disable Link while animated Basket
but i can understand where i place this code.. anybody help me to fix it please ...
$(document).ready(function(){
$("#basketItemsWrap li:first").hide();
$(".productPriceWrapRight a img").click(function() {
var productIDValSplitter = (this.id).split("_");
var productIDVal = productIDValSplitter[1];
var productX = $("#productImageWrapID_" + productIDVal).offset().left;
var productY = $("#productImageWrapID_" + productIDVal).offset().top;
if( $("#productID_" + productIDVal).length > 0){
var basketX = $("#productID_" + productIDVal).offset().left;
var basketY = $("#productID_" + productIDVal).offset().top;
} else {
var basketX = $("#basketTitleWrap").offset().left;
var basketY = $("#basketTitleWrap").offset().top;
}
var gotoX = basketX - productX;
var gotoY = basketY - productY;
var newImageWidth = $("#productImageWrapID_" + productIDVal).width() / 3;
var newImageHeight = $("#productImageWrapID_" + productIDVal).height() / 3;
$("#productImageWrapID_" + productIDVal + " img")
.clone()
.prependTo("#productImageWrapID_" + productIDVal)
.css({'position' : 'absolute'})
.animate({opacity: 0.4}, 100 )
.animate({opacity: 0.1, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 1200, function() {
$(this).remove();
$("#notificationsLoader").html('<img src="images/loader.gif">');
$.ajax({
type: "POST",
url: "inc/functions.php",
data: { productID: productIDVal, action: "addToBasket"},
success: function(theResponse) {
if( $("#productID_" + productIDVal).length > 0){
$("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
$("#productID_" + productIDVal).before(theResponse).remove();
$("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
$("#productID_" + productIDVal).animate({ opacity: 1 }, 500);
$("#notificationsLoader").empty();
} else {
$("#basketItemsWrap li:first").before(theResponse);
$("#basketItemsWrap li:first").hide();
$("#basketItemsWrap li:first").show("slow");
$("#notificationsLoader").empty();
}
}
});
});
});
$("#basketItemsWrap li img").live("click", function(event) {
var productIDValSplitter = (this.id).split("_");
var productIDVal = productIDValSplitter[1];
$("#notificationsLoader").html('<img src="images/loader.gif">');
$.ajax({
type: "POST",
url: "inc/functions.php",
data: { productID: productIDVal, action: "deleteFromBasket"},
success: function(theResponse) {
$("#productID_" + productIDVal).hide("slow", function() {$(this).remove();});
$("#notificationsLoader").empty();
}
});
});
});
I think what you might have to-do is at the end of the click event function then unbind the event http://api.jquery.com/unbind/ $(this).unbind();
You could possibly bind it again once the ajax has finished
I am currently trying to upload a file through a form I made, using jQuery. This is what I am attempting to do:
function ajax_upload(){
var formData = new FormData($('form#userform'));
$.ajax({
url: location.protocol + "//" + location.host + "/profile/uploadPicture",
dataType: 'json',
data: formData,
type: 'post',
success: function(data, status){
if (data.status == 'error') {
$('#error').html(data.msg);
}
else {
var filename = location.protocol + "//" + location.host + "/data/profiles/" + data.msg;
$('input#filename').val(filename);
close_upload_form();
pop_profilechanger(filename);
}
}
});
return false;
}
the form appears to post to the backend, but while trying to return the filename from the uploaded file in a JSON object like:
echo json_encode(array('status' => $status, 'msg' => $msg));
Is there something wrong?
Please tell me
This is how I do it. If you want more information, rather than just my pasted code, check out this http://www.html5rocks.com/en/tutorials/file/dndfiles/ because my code was made for drag and drop files
handleFiles : function(files, evt, target)
{
console.log(target);
$.each(files, function(index, value)
{
var file = value;
reader = new FileReader();
reader.onload = function(evt)
{
var li = $("<li class='uploading'><img data-image-id='0' src='" + evt.target.result + "' /></li>");
target.find('.imagesList').append(li);
var request = new XMLHttpRequest();
var formData = new FormData();
formData.append('image', file);
request.open("Post", settings.uploadImageUrl);
request.addEventListener("load", function(evt)
{
var id = evt.target.responseText;
li.removeClass('uploading');
li.find('img').attr('data-image-id', id);
}, false);
request.send(formData);
};
reader.readAsDataURL(value);
});
},