The ajax result is not returned - javascript

I'm trying to import MySQL data(select query) using spring, mybatis.
In js, call the controller function via ajax, and get DB data from the controller.
ex.
ajax
url: /testmysql
controller
requestmapping: /testmysql
return mav (modelandview)
sysout(mav) is good for the controller.
But Ajax is not found(404) in js.
I was told that an ajax 404 error comes out when there is no return value.
But what should I do now?

You are making ajax call without root context url of your application.
$.ajax({
url: '/context/testmysql',
type:'GET',
success: function(data) {
console.log(data);
}
});

Related

How to use file response in Ajax query

//returning File Response in Controller action Method
return File(MyMemoryStream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "lp.xlsx");
I want to download this file but I have used ajax for passing data from view to Controller so it's returning to Ajax success function .
<script>
$(".pfamlink").click(function()
{
$.ajax({
type: "GET",
url: "/Home/Excel",
data: { "data":$(this).html()},
success: function(response)
{}
});
});
</script>
what I need to give in this function(response) to download returning file from controller?
or any suggestions for passing table data from view to controller without Ajax ?
what I need to give in this function(response) to download returning
file from controller?
The FileContentResult is returned to the Ajax success function, the download was unsuccessful because we need to operate in success. Try to add an action to download, use window.location to redirect to the Download action in controller.
public async Task<IActionResult> DownloadAsync()
{
...
//returning File Response in Controller action Method
return File(MyMemoryStream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "lp.xlsx");
In your ajax success, change your code like below:
success: function (data) {
window.location = '/yourcontrollername/Download';
}

Configure Ajax call for Server-side filtering of Data from Json Rest API

I use ajax jquery call to fetch data about tests from Jenkins test report's REST API. However, I want only those tests for which 'status' is not PASSED and FIXED. Now, can I configure my Ajax call such that this filtering is already done on the server side, so that passed tests are not returned as a part of the response ? My Ajax call so far :
function getTestResultsForJob(jobTestResultsUrl){
var listOfFailures = {};
$.ajax({
type: 'GET',
dataType: 'json',
url: jobTestResultsUrl,
async: false,
error: function() {
alert("Request has failed for " + jobTestResultsUrl);
},
success: function(data){
console.log('Request is success for ' + jobTestResultsUrl);
listOfFailures = data;
}
});
return listOfFailures;
}
It isn't possible to do such filtering with json on the server side.
The following returns the build numer and result:
job/Test/api/json?tree=builds[number,result]
And doing the filtering inside the success method of you ajax call.
If you can switch to xml the query would be like that:
job/Test/api/xml?tree=builds[number,result]&exclude=mavenModuleSet/build[result="PASSED"]

CodeIgniter Access Controller from PHP Require

I am using codeigniter for my project and I am stuck trying to figure this out.
I have some javascript that needs to perform an AJAX call to fetch some results based on a dropdown value that was selected.
function fetchLines(){
$.ajax({
url: baseURL + "resources/ajax.php?node=fetchLines",
type: 'GET',
cache: false,
data: {
lineType: 'business'
},
error: function(err) {
alert(err.statusText);
},
success: function(data) {
console.log(data);
}
});
}
In this AJAX file, I am trying to include my controller and then access the function within it.
<?php
define('BASEPATH', "AJAX");
require_once('../application/controllers/Project.php');
switch($_REQUEST['node']){
case 'fetchLines':
$objLines = new Project();
$objLines->fetchLines($_REQUEST['lineType']);
break;
}
?>
My CI Controller then has a private function in it which I am trying to call to get the data I need:
private function fetchLines($lineType){
$lines = $this->project_model->fetchLines($lineType);
return $lines;
}
My goal here is to have an AJAX file or controller (if needed) be used for all my AJAX calls. It needs to be able to access a controller and return data.
With the current code above, I am getting the error: Class 'CI_Controller' not found in <b>C:\xampp\htdocs\blueprint\application\controllers
\Project.php
Is there a better way to handle situations like this? I'm not an expert with OOP but some reading suggested something along these lines.
why you are not sending this request to the controller method instead ?
function fetchLines(){
$.ajax({
url: baseURL + "controller-name/method-name",
type: 'GET',
cache: false,
data: {lineType: 'business'},
error: function(err) {
alert(err.statusText);
},
success: function(data) {
console.log(data);
}
});
}
NOTE and in controller you can access these values as
function method-name(){
echo $this->input->get('lineType');
}
Lets say my ajax file is in the controllers folder
And I want to re-use my controllers, i would do it like this :
$this->load->library('../controllers/your_controller');
$this->your_controller->_some_method($data);
Load the controller as library and used it like a library.
Hope this helps.
You should put the code that listens to AJAX call in a controller function only. The way you are trying to do is not a good practice at all.
If you want a method to be executed only if the request was an XHR i.e.AJAX request then use
if($this->input->is_ajax_request()){
//your code
}else{
redirect(base_url())
}

AJAX call without success field?

Hi all is it possible to call a an ajax call without using success?
ie from:
$.ajax({
type: "POST",
url: "/project/test/auto",
data: data,
success: function(msg){
//window.location.replace(msg);
}
});
to something simply like:
$.ajax({
type: "POST",
url: "/project/test/auto",
data: data,
});
To give reasoning - I have written some php to insert items into a database and then redirect to another page, I don't want to have to re-write the same code again to insert into the DB and then redirect in JS.
jQuery.post() documentation
$.post( "/project/test/auto", data );
I don't think that you can redirect using PHP within ajax call.
It would be best to create a function out of DB insert and then with ajax call a page that executes that function and returns success:true in json for example. After that, redirect in success part of your ajax call. You can later call the same DB insert function from within your PHP code.

Call web service in javascript template

Is it possible to call a Web Service inside an .ejs file and print the returned value?
Like in python:
req =requests.get(link, timeout=20)
response = req.text
data = json.loads(response)
Try with Ajax request.
$.ajax({
url: "urltoyourservice.xml"
dataType: "xml",
}).done(function(data) {
console.log(data);
});

Categories

Resources