How to reload page using Ajax - javascript

I have function addFeaturesToScene which make a new URL using ajax.
After the function ends I need to go to the new site automatically
Here is code:
function addFeatureToScene(featureNid, sceneNid)
{
alert(featureNid + " -> " + sceneNid);
$.ajax({
type : 'GET',
url : '/copy-feature-and-add-it-to-scene/' + featureNid + '/' + sceneNid,
dataType : 'json',
async : false,
success : function(reply) {
//Code to open a page with a new url?
}
});
}

You can use window.location
window.location.href = 'http://example.org';

Related

I want to set a url inside my ajax call back function

I want to make a clickable link in my ajax success response. But I could not do this.
<td id="attachment"></td>
function DoAction(id) {
$.ajax({
type: "get",
url: "/view_message",
data: "id=" + id,
dataType: 'json',
success: function(data) {
if (data) {
var text = "No Files There !";
$('#myModal').modal('show');
$('#subject').text(data.subject);
$('#body').text(data.body);
$('#created_at').text(data.created_at);
if (data.attachment) {
$('#attachment').html('<a href="files/' + data.attachment + '" />click</a>');
} else {
$('#attachment').text(text);
}
}
}
});
}
I want to display a clickable link in my .
I always encounter this when using ajax calls so;
Update your code from this.
$('#attachment').html('<a href="files/' + data.attachment + '" />click</a>');
To This.
$(document).find('#attachment').html('<a href="files/' + data.attachment + '" />click</a>');
if the code doesn't work add a console.log("working") function to see if your code is really reaching the success function.
I hope it helps.

jQuery AJAX URL change Phonegap

How do I change the URL in Ajax and reload page with new content?
I make a service in WP: www.test.com/page/?u=test
$_GET['u'];
and while(..... new information) works good.
But I have a problem with this..
$('#test').click(function(){
uel = "http://......com/servisi302/?u=zanimljivosti";
$('#page-content').hide(1000);
$('#page-content').show(1000);
alert(uel);
return false;
});
jQuery.ajax({
url : '' +uel,
type : "POST",
dataType : "json",
data : "param=no",
success : function(html){
var QUERY = jQuery(".query");
console.log(html);
jQuery.each(html, function(key, value){
console.log(value);
QUERY.append("<div class='media well'><a href='#' class='pull-left'><img width='64' height='64' src='" + value.slika + "'class='media-object' alt='' /></a><div class='media-body'><h4 class='media-heading'><p>" + value.naslov + "</p></h4></div></div>");
});
}, error : function(e){
alert(e);
}
});
});
How do I change the URL or variable and subsequently reload with the new content?

Ajax Request on another's success ( jQuery )

I want to run the another ajax request on one's success.
THE CODE
$(document).ready(function(){
$('*[data-load-template]').each( function () {
var template = $(this).data('load-template');
var element = $(this);
$.ajax({
url : "templates/" + template + '.tpl',
method : "GET",
success : function (htmlData) {
$.ajax({
url : "data/" + template + '.json',
method : "GET",
success : function (jsonData) {
console.log(jsonData); // why isn't this one working???
}
});
element.html(htmlData); // this is working
}
});
});
}); // dom ready event
My question is why isn't this code working and how to make it work.

ajax success event doesn't work after being called

After searching here on SO and google, didn't find an answer to my problem.
The animation doesn't seem to trigger, tried a simple alert, didn't work either.
The function works as it is supposed (almost) as it does what i need to, excluding the success part.
Why isn't the success event being called?
$(function() {
$(".seguinte").click(function() {
var fnome = $('.fnome').val();
var fmorada = $('.fmorada').val();
var flocalidade = $('.flocalidade').val();
var fcodigopostal = $('.fcodigopostal').val();
var ftelemovel = $('.ftelemovel').val();
var femail = $('.femail').val();
var fnif = $('.fnif').val();
var fempresa = $('.fempresa').val();
var dataString = 'fnome='+ fnome + '&fmorada=' + fmorada + '&flocalidade=' + flocalidade + '&fcodigopostal=' + fcodigopostal + '&ftelemovel=' + ftelemovel + '&femail=' + femail + '&fnif=' + fnif + '&fempresa=' + fempresa;
$.ajax({
type: "GET",
url: "/ajaxload/editclient.php",
data: dataString,
success: function() {
$('.primeirosector').animate({ "left": "+=768px" }, "fast" );
}
});
return false;
});
});
you are trying to pass query string in data it should be json data.
Does your method edit client has all the parameters you are passing?
A simple way to test this is doing the following:
change this line to be like this
url: "/ajaxload/editclient.php" + "?" + dataString;
and remove this line
data: dataString
The correct way of doing it should be, create a javascript object and send it in the data like so:
var sendData ={
fnome: $('.fnome').val(),
fmorada: $('.fmorada').val(),
flocalidade: $('.flocalidade').val(),
fcodigopostal: $('.fcodigopostal').val(),
ftelemovel: $('.ftelemovel').val(),
femail: $('.femail').val(),
fnif: $('.fnif').val(),
fempresa: $('.fempresa').val()
}
$.ajax({
url: "/ajaxload/editclient.php",
dataType: 'json',
data: sendData,
success: function() {
$('.primeirosector').animate({ "left": "+=768px" }, "fast" );
}
});
Another thing shouldn't this be a post request?
Hope it helps

javascript window.print() access is denied

I'm loading an image from a different domain in a img html element via $.ajax cross domain call and opening up a popup with the contents of the image as you can see in the code below
var printWindow= "";
methodName = "CreateVisitor";
$.support.cors = true;
$.ajax({
type: "get",
url: visitorServiceUrl + methodName + "/" + tenantId + "/" + userId
+ "/" + visitorid + "/" + visitorname + "/" + visitormobilenumber
+ "/" + visitorcompanyid + "/" + visitorcompanyname + "/" + visiteeid,
dataType: "jsonp",
async: false,
cache: false,
crossDomain: true,
success: function (data) {
var imageUrl = "";
imageUrl = data ? siteImageUrl + data : 'image/NO_IMAGE.jpg';
$("#visitorcard").attr('src', imageUrl);
printWindow = window.open($("#visitorcard").attr('src'), "_blank", 'width=200,height=200');
// printWindow.onload = function () {
// printWindow.print();
// };
setTimeout(function () { printvisitorcard(); }, 1000);
},
error: function (xhr, type, exception) {
//alert("Error: " + xhr + " : " + exception + " : " + type);
}
});
I then use a setTimeout() to try to automatically print the image to my network printer (I'm not sure if I'm in the same domain or not) like this
function printvisitorcard() {
printWindow.print();
printWindow.close();
}
However each time I'm getting a Microsoft JScript runtime error: Access is denied. on the printWindow.print(); line. I'm aware of cross domain issue with javascript but I still want to print the image. How do I overcome this error? Any help will be appreciated.
P.S
Firefox and Chrome do not throw this error however they also do not print the image.
change to something from your own domain.
Here is a DEMO - works in Fx - to work in Chrome, you need to add a click handler. Popups are blocked unless the user clicked to get it
success: function (data) {
var imageUrl = data ? siteImageUrl + data : 'image/NO_IMAGE.jpg';
$("#visitorcard").attr('src', imageUrl);
var printWindow = window.open("", "_blank", 'width=200,height=200');
printWindow.document.write('<body onload="window.focus();window.print()">'+
$("#visitorcard").parent().html()+'</body>');
printWindow.document.close();
},

Categories

Resources