Ajax call url in an external js file - javascript

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

Related

retreiveing website content after scripts injection

I'm trying to get a website content but when I use ajax...
$.ajax({
url: url,
type:'GET',
async: false,
success: (data) => websiteContent = data
});
I get only something like <div class="root"></div>. Is there a way to retreive a full webpage code after running all scripts?

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

Loading data asynchronous with javascript does NOT work?

I want to transfer data via JSON and receive it in a browser via ajax. The data size is about 2 mb and the problem is that firefox does not respond during receivement of the data.
So I want to receive the data in the background, without interrupting the webinterface, but it does not seem to work.
My javascript code
$.ajax({
url: 'http://127.0.0.1:10085/data/getd',
dataType: 'json',
global: false,
success: function (response) {
// just for testing
alert("HI");
},
async: true,
});
Maybe you can help me?
Regards
Marko

jQuery Ajax POST JSON issue

I'm trying to pass a JavaScript array into a PHP script to then be put into a database but I'm having issues. I have used JSON.stringify on the array before sending. The request seems to work in that when stepping through the code debugging the php page is called but no data is passed into the POST global. I'm sure its something amateurish that I've missed but I'm struggling. This is the code:-
$.ajax({
type: "POST",
datatype: "json",
url: "processdraw.php",
data: {
json: pDrawnTeams
},
contentType: "application/json; charset=utf-8",
success: alert('worked')
})
If you are not getting any error in your javascript, make sure that you are getting that parameter like this in your php target file:
$myJSON = $_POST['json']; // ['parameter name']
after that you will need to decode that json
$myData = json_decode($myJSON, true) // true is for retrieving as an array

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