简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM