Ajax request inside ajax request with Laravel - javascript

I have a code that works like this:
$.ajax({
type: "POST",
url: url,
data: formData,
cache: false,
processData: false,
contentType: false,
dataType: 'html',
This loads:
Route::post('/local/pdf', 'LocalController#subirPDF');
Which loads the controller:
return View::make(Config::get('rv.paginasPDF'));
Which loads:
$.get("{{Request::root()}}/local/actualizar_paginas/9/2");
Which loads the route:
Route::get('/local/actualizar_paginas/{id}/{paginas}', 'LocalController#actualizarPaginas')->where('id', '[0-9]+')->where('paginas', '[0-9]+');
That loads:
Auth::user()->perfil='penadaaaaaafe';
Auth::user()->save();
Don't worry if you see that I don't use some variables, because I have simplified the code to make it easier to explain.
Basically, if I load directly the route from the url everything works fine:
{{Request::root()}}/local/actualizar_paginas/9/2"
But if I load it with the ajax method that I have devoloped it doesn't work. If I console.log the data I recieve the right html code of the $.get page, but It doesn't make the get request.
Any idea how to fix it?
Thanks.

Related

Open file upload dialog after Ajax post success

I have functionality in which it is required to open file upload dialog after Ajax call success event.
What I tried:
I tried applying below simple code in ajax success: and complete: event but it is not working.
$.ajax({
url: url,
type: 'GET',
dataType: 'json',
data: { id: eoid },
contentType: 'application/json; charset=utf-8',
success: function (data) {
// some logic
$("#fileupload").click();
}
});
What is problem:
If I put simple button and try to execute above code, it is working fine and opening dialog - but it is not working in case of ajax post afterwards.
Any guesses or am I missing something?
Thank you.
The problem is at dataType: 'json' . You are loading html with your ajax request so you should change it to dataType: 'html' else in any other format it will not be considered success. Or you can delete this property as stated in Jquery doc that Jquery does default: Intelligent Guess (xml, json, script, or html).

Grails GSP Ajax Call OnSuccess not called

I am performing an ajax call from a .gsp file in grails:
$.ajax({
async: false,
url: '<g:createLink controller="mycontroller" action="myaction"/>',
data: params,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
onSuccess: 'toggleSaveButton(false);'
});
mycontroller
def myaction() {
// do some funky stuff with params
// params are available, everything here works without a problem
}
outcome
the ajax call is performed and the controller function is called correctly with all attached data.
issue
my onSuccess: is ignored and never called
i already tried
using the more generic onComplete
change the onSuccess: to function(){toggleSaveButton(false);}
render (true as JSON) in my controller action
I believe it should be success: instead of onSuccess:, according to JQuery ajax docs.
To demonstrate:
http://jsfiddle.net/bL60Lta9/2/
Rewriting to :
onComplete: dataUpdatedOnSuccess()
did the trick.

redirect with javascript to another domain with Json

I'm retrieving an url from a php file with Json , and then in the success part I do
window.location=msg.message
but the proble is that if we suppose my domain is http example.com and the
msg.message value is
https://otherdomain.com
the redirection is done to http:/example.com/https:/otherdomain.com
how can I go directly to https:/otherdomain.com
code
$.ajax({
type: "POST",
url: "ajax.php",
data: dataString,
dataType: "json",
success: function (msg){
$.fn.colorbox.close();//close the box
alert(decodeURI(msg.message));//
window.location.href(msg.message); // goes to domain.com/msg.message
},
});
Please use assign method:
window.location.assign("https://otherdomain.com")
You need two stashes before the host part of the URL.
https://otherdomain.com
You have only one (https:/otherdomain.com).
(And href is a string, not a function, assign a value to it as you do in the first code block, don't try to call it as you do in the third).

window.open when ajax call end, doesn't work

i'm doing and Ajax call to send parameters to one .php file, when the call is done, i want to popup a new window to the results page, but it doesn't work.
here is my ajax code
$.ajax({
type: "POST",
url: "pipeline2.php",
data: formData,
cache: false,
processData: false,
contentType: false,
//async: false,
}).done(function (data) {
$("#resultados_pipeline").append(data);
document.getElementById('Running').style.display = 'none';
window.open("results_page.php?jid="+job_id);
alert(job_id);
});
i first think that maybe the job_id is not set in ajax, but the alert show me the expected...
i also tried with the async: true, and it doesn't work either
Browsers don't allow window.open calls except when involved from a user triggered event.
An Ajax response event is not something triggered by the user, so popups are blocked at that point.

Making an AJAX call from included JavaScript File?

I have one JSP File and one JS File .
So inside my JSP File , i have included the JS (Javascript File) like this
<script type="text/javascript" src="HumbleFinance.js"></script>
As part of my JSP i have Inside JSP File , I have
jQuery.ajax({
url: '/HumblFin/Serv',
type: 'GET',
contentType: 'application/json',
dataType: 'json',
timeout: 5000,
success: function(data) {
drawChart(data);
}
Now my question is , From the included JS File , how can i make a call to the jQuery.ajax( function ?? which has been defined in JSP File ??
Please advice
Just call it. The only requirement is the the <script> element that loads the functions you want must be loaded into the document before you try to call those function.
The same way you added the ajax call. It can be something like this:
function callAjax(data){
jQuery.ajax({
url: '/HumblFin/Serv',
type: 'GET',
contentType: 'application/json',
data: data,
dataType: 'json',
timeout: 5000,
success: function(data) {
drawChart(data);
}
}
Now you can call the function callAjax() anywhere you want. Obviously inside a javascript file or <script type="text/javascript">callAjax();</script> if you're using inline javascript. PS> I've added data as a parameter. Now you can pass the data to the function and it will be passed to the server through ajax call.

Categories

Resources