How to loop the ajax call for pdf file download? - javascript

if( $.isArray(certTypeReq) ){
$.each(certTypeReq,function(key,certType){
$.ajax({
url: baseUrl+"/user/certificate/getlink",
type: 'GET',
data: { 'certType' : certType},
dataType : 'json',
cache: false,
async:false,
success: function(data) {
window.location.href = data.link;
}
});
});
}
This is my code and its working fine in Firefox but not in chrome browser. If anyone has solution please help me.

var i=1;
if( $.isArray(certTypeReq) ){
$(certTypeReq).each(function(key,certType){
setTimeout(function () {
$.ajax({
url: baseUrl+"/user/certificate/getlink",
type: 'POST',
data: { 'certType' : certType},
dataType : 'json',
success: function(data) {
window.location.href = data.link;
}
});
}, 3000*i);
i++;
});
}
Applied delay in each iteration and it worked.
Thanks to all for your replies.

Related

Getting array data from PHP to jQuery for outputting

Here is the partial code from the remove PHP file:
if($action == 'trackings_get') {
$result = $trackings->get(getCourierSlugByID($GLOBALS['tracking_id']), $GLOBALS['tracking_id']);
$result_history = $result['data']['tracking']['checkpoints'];
echo json_encode($result_history);
// debugging
//pretty_print($result_history);
}
Here is the JS from the remote site i am trying to call the data for:
$.ajax({
url: '/login/tracking.php',
type: 'POST',
dataType: "json",
data: {
action: action,
tracking_id: tracking_id
},
success: function(json){
//debug
alert(JSON.stringify(json));
}
});
try this
function test(){
$.ajax({
url: 'url',
type: 'POST',
dataType: "json",
data: {
action: action,
tracking_id: tracking_id
},
success: function(json){
}
});
}
i try this code in inspect element in this page https://tracking.ambientlounge.com/
function test(){
$.ajax({
url: 'your url',
type: 'POST',
dataType: "json",
data: {
action: "action",
tracking_id: "tracking_id"
},
success: function(json){
//debug
console.log(json);
}
});
}
result is array . dont need JSON.stringify .

Ajax not being called in javascript function

In the success function I want to call a function. The problem is that ajax does not fire, so the data is never triggered and display. Here is my ajax call with a javascript function call in the success function.
$.ajax({
type: "POST",
url: "./api/login.php",
data: dataString,
cache: false,
success: function(data){
if(data){
//FUNCTION CALL WHEN USER LOGGING IN
retrieveUserBlogData();
window.location = "api/home.php";
}else{
$('.alert').show();
}
}
});
function retrieveUserBlogData(){
$.ajax({
type: "GET",
url: 'retrievePostData.php',
data: "",
dataType: 'json',
success: handleData
});
}
function handleData(data) {
alert(data);
var blog_file = data[3];
$('#imageDiv')
.append('<img id="blog_img" src="upload/' + blog_file + '"><br>');
}
I cant figure out why the ajax in the retrieveUserBlogData() function is not being triggered.
Any help would be appreciated Thanks.
Even if the AJAX succeeds, you are redirecting the browser to a different page after the first AJAX request:
window.location = "api/home.php";
So I would suggest removing that.
Try the following code for redirecting to another window
window.location.assign(URL);
then it may work.
Try it like this
$.ajax({
type: "POST",
url: "./api/login.php",
data: dataString,
cache: false,
success: function(data){
if(data){
//FUNCTION CALL WHEN USER LOGGING IN
retrieveUserBlogData();
}else{
$('.alert').show();
}
}
});
function retrieveUserBlogData(){
$.ajax({
type: "GET",
url: 'retrievePostData.php',
data: "",
dataType: 'json',
success: function(data){
alert(data);
var blog_file = data[3];
$('#imageDiv')
.append('<img id="blog_img" src="upload/' + blog_file + '"><br>');
window.location = "api/home.php";
}
});
}

How can i redirect page JavaScript/PHP?

I want to redirect page to URL that return from PHP script.
That when user created a post i want to return value of mysql_insert_id() for post_id.
$('input#submitButton').click( function() {
$.ajax({
url: 'newpost.php',
type: 'post',
dataType: 'json',
data: $('form#myForm').serialize(),
success: function(data) {
window.location.replace("http://localhost/?post_id=...");
}
});
});
I looked here but not found what i want.
Link
In your php script do :
echo json_encode(array(
'id' => $THE_ID
));
then here do:
$('input#submitButton').click( function() {
$.ajax({
url: 'newpost.php',
type: 'post',
dataType: 'json',
data: $('form#myForm').serialize(),
success: function(data) {
window.location = "http://localhost/?post_id=" + data.id;
}
});
});
});

JSP AJAX return NULL

i using JQuery Ajax to call REST api in jsp but it return null no matter how i call but it can work in html. is there any way to solve this problem. Cant seem to find a solution in the net.
$(document).ready(function () {
alert('ready');
var accessKey = 'xkg8VRu6Ol+gMH+SUamkRIEB7fKzhwMvfMo/2U8UJcFhdvR4yN1GutmUIA3A6r3LDhot215OVVkZvNRzjl28TNUZgYFSswOi';
var thisUrl = 'http://www.onemap.sg/API/services.svc/getToken?accessKEY=' + accessKey;
$.ajax({
type: "GET",
url: thisUrl,
dataType: 'application/json',
success: function (data) {
alert('data is:' + data.GetToken[0].NewToken);
}
});
alert(thisUrl);
});
dataType should be jsonp
$(document).ready(function () {
var thisUrl = 'http://www.onemap.sg/API/services.svc/getToken?accessKEY=' + accessKey;
$.ajax({
type: "GET",
url: thisUrl,
dataType: 'jsonp',
success: function (data) {
console.log(data)
alert('data is:' + data.GetToken[0].NewToken);
}
});
});
Refer to this article:
http://www.isgoodstuff.com/2012/07/22/cross-domain-xml-using-jquery/
You only need "jquery.xdomainajax.js" that is there in the sample source-code to make it work.
$.ajax({
url: 'https://asdf/asdf',
dataType: "xml",
type: 'GET',
success: function(res) {
var myXML = res.responseText;
alert(myXML);
}
});

Google Chrome: SyntaxError: Unexpected end of input

I keep on getting this error on chromium but not on Firefox n its driving me nuts because I cant find a solution despite searching for hours now. I basically get JSON from the server and then wanna insert it into DOM. Here's my code...
function lookup(inputString){
if(inputString.length == 0){ //hide suggestions
$('#suggestions').empty()
.fadeOut();
}
else{ //make an AJAX call
$.ajax({
type: 'GET',
url: '{% url "search.views.search" inputString="xyz" %}'.replace("xyz", inputString.toString()),
dataType: 'json',
success: function(search_results){
suggestions = JSON.parse(JSON.stringify(search_results));
alert(suggestions[0].name);
}
})
}
}
You need a ; at the end of this:
$.ajax({
type: 'GET',
url: '{% url "search.views.search" inputString="xyz" %}'.replace("xyz", inputString.toString()),
dataType: 'json',
success: function(search_results){
suggestions = JSON.parse(JSON.stringify(search_results));
alert(suggestions[0].name);
}
});
same conclusion :)
you missed a semicolon ;
jslint could help you to find this kind of errors : http://www.jslint.com/
function lookup(inputString) {
if (inputString.length === 0) {
$('#suggestions').empty().fadeOut();
}
else {
$.ajax({
type: 'GET',
url: '{% url "search.views.search" inputString="xyz" %}'.replace("xyz", inputString.toString()),
dataType: 'json',
success: function(search_results) {
suggestions = JSON.parse(JSON.stringify(search_results));
alert(suggestions[0].name);
}
});
}
}

Categories

Resources