Making an AJAX call from included JavaScript File? - javascript

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.

Related

Ajax call url in an external js file

I have a web page (written in php / codeigniter) that uses some javascript code.
I tried to move some javascript code to external file to better manage the code.
So in document ready event I load external file with
$.getScript('<?php echo base_url();?>assets/js/offerta_editScontirow.js');
one piece of code, putted in an external js file, does not work; instead it works correctly if I put all the code inside the main php file.
$.ajax({
dataType: 'json',
url: '<?php echo site_url("Offerta/applica_sconti");?>',
cache: false,
data: data,
success: function (data, status, xhr)
{
........
It does not work because it does not elaborate the php code, and in the query string I find
"php echo site_url("Offerta/applica_sconti");?>:"
So, is there a way to have it works? May I pass any parameter to the external js file while loading it, and pass the url to be used in ajax call? Some other method?
Kind regards,
Matt
try this window.my_url = 'some value'; and get the variable like below
window.my_url = '<?php echo site_url("Offerta/applica_sconti");?>';
$.getScript('<?php echo base_url();?>assets/js/offerta_editScontirow.js');
and
$.ajax({
dataType: 'json',
url: my_url,
cache: false,
data: data,
success: function (data, status, xhr)
{

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).

Ajax request inside ajax request with Laravel

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.

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).

Can I bind asp.net dropdownlist using javascript/jquery?

Can I bind asp.net dropdownlist using javascript/jquery? I get the data from jquery ajax from a web method so I want to avoid postback at this point. But I still want to do postback and save all the data using server side code (i.e. I still be able to do this dropdownlist1.selecteditem.text) after binding it using clientscripts.
Is it possible and can someone explain me how it can be done?
Thanks,
Use Json ajax
Syntax:
$.ajax({
dataType: "json",
url: url,
data: data,
success: success
});
See simple example for json:
<script type="text/javascript">
$(document).ready(function () {
var msgbox = $("#status");
$("#Button1").click(function () {
$.ajax({
type: "POST",
//Page Name (in which the method should be called) and method name
url: "BeginJson.aspx/CheckDateTime",
// If you want to pass parameter or data to server side function you can try line
// data: "{}",
//else If you don't want to pass any value to server side function leave the data to blank line below
data: "{'args':'Somnath'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
//Got the response from server and render to the client
msgbox.html(msg.d);
}
});
});
});

Categories

Resources