Sinatra & Angular $HTTP.delete 404 - html

Trying to delete from my API which is hosted on Heroku.
In Ruby file CROSS-ORIGIN is enable and I can get information from my API.
Although I have a problem when I want to make a delete request to the server.
I get an error 404 (Page not found), but when typing the url into the browser myself I can easily get this page.
cURL call works fine without any problems too.
script.js
$scope.DeleteData = function (index) {
$scope.id = $scope.companies[index].companyID;
var deleteUrl = 'https://*****.herokuapp.com/api/v1/companies/' + $scope.id;
$http.delete(deleteUrl,'DELETE').then(function(response){
console.log(response);
},function(errorResponse){
console.log(errorResponse);
});
};
Ruby.rb
delete '/companies/:companyID' do
tempCompanyID = params['companyID']
company = Company.where(companyID: tempCompanyID).first
company.destroy
end
Html:
<div ng-app="myApp" ng-controller="companyCtrl">
<table class="table table-striped table-hover">
<thead class="thead-dark">
<th>Company ID</th>
<th>Company Name</th>
<th>Address</th>
<th>City</th>
<th>Country</th>
<th>Owners</th>
<th></th>
</thead>
<tr ng-repeat="company in companies">
<td>{{company.companyID}}</td>
<td>{{company.companyName}}</td>
<td>{{company.address}}</td>
<td>{{company.city}}</td>
<td>{{company.country}}</td>
<td>{{company.owners}}</td>
<td><button class="btn" name="_method" ng-click="DeleteData($index)">Delete</button></td>
</tr>
</table>
</div>
Chrome response:
The response

Related

display data using Ajax in laravel

I'm trying to fetch the data from the database from a table 'Company' in the output it coming as a json format,i have to display it in a Data Table.
Controller
public function ex(Request $request){
$table = \DB::table('company')->select('name','type','address','city','email','description')->get();
//return view('mydata')->with('company',$table);
return response($table);
}
Route
Route::get('display','Test#ex');
Blade page
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("table").toggle();
$.get("{{URL::to('display')}}",function (data) {
$.each(data,function (i,value) {
var tr =$("<tr/>");
tr.append($("<td/>",{
text : value.name /*Get first column*/
}))
})
})
});
});
</script>
</head>
<body>
<button>Click Me</button>
<table border="1px" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Address</th>
<th>City</th>
<th>Email</th>
<th>Description</th>
</tr>
</thead>
</table>
</body>
</html>
the output coming as in following image but i want to display it in a Datatable
you can use yajjra laravel package to show data via ajax
here is the complete tutorial to show data via ajax in datatable
Datable Tutorial

Method 'load' does not work bootstrap-table

I'm trying to load data and popular a table bootstrap-table through an ajax call, but it does not work with the load method. Trying the data method works normally.
the method is not starting, it also does not display that no result was found
Table
<table id="table-anexo" class="table table-striped table-hover display" style="width:100%">
<thead>
<tr>
<th data-field="id"><span>DOCUMENTO</span></th>
<th data-field="tipo"><span>TIPO</span></th>
<th data-field="dt_anexo"><span>DATA ANEXO</span></th>
<!--<th data-field="acao" data-formatter="" data-events="" ><span>AÇÃO</span></th>-->
</tr>
</thead>
</table>
Function AJAX
function get_anexo_ajax(metodo, editando_id) {
var ticket_id = editando_id;
$.ajax({
type: 'POST',
data: {ticket_id : ticket_id},
url: 'ticket/get_anexo_all',
success: (function (data) {
console.log(data.anexo);
//$('#table-anexo').bootstrapTable({ data: data.anexo });
//$('#table-anexo').bootstrapTable('refresh')
$('#table-anexo').bootstrapTable('load', data.anexo);
}),
});
}
Data JSON
{"anexo":[{"id":"1","tipo":"jpg","dt_anexo":"2018-08-01 11:09:28","mensagem_id":"2","ticket_id":"1"}]}
I think data is the invalid format. And Set data-toggle="table" on a normal table. I tested, it run Ok. You can try:
<table id="table-anexo" data-toggle="table" class="table table-striped table-hover display" style="width:100%">
var obj = JSON.parse(data);
$('#table-anexo').bootstrapTable('load', obj.anexo);

DataTables initialization using html attribute

I have table
<table class="table dataTable table-striped table-bordered"
cellspacing="0" width="100%"
data-toggle="table"
data-processing="true"
data-serverSide="true"
data-ajax="#Url.Action("GetUsers", "Users")"
data-type="post"
data-page-length="25"
data-order="[[0,"asc"]]"
>
<thead>
<tr>
<th>ID</th>
<th>Role</th>
<th>User name</th>
<th>Date registered</th>
<th></th>
</tr>
</thead>
</table>
my js where init table
$(document).ready(() => {
let table = $('[data-toggle="table"]');
if (table.length > 1) {
table.DataTable();
}
});
I sees request ajax to GetUsers, but without parameters. What I should to do for fixed it? I should using more html attributes, or I should using js?

Pagination not working when Dynamic result is displayed to datatable <tbody>

using a datatable (bootstrap )
i am displaying the result of ajax in part of datatable . the result ged addded to datable but
pagination not working on new result as the page loads only once.Who the pagination will work on new displayed result.
Thanks in advance.
<script>
$(document).ready(function () {
$('.dataTables-example').dataTable();
});
var data = $.parseJSON(responseData);
var tbl_op ='';
$.each(data.result,function(k,v){
console.log(v.id);
$('.dataTables-example').dataTable();
tbl_op +="<tr class='odd gradeX'>"+
'<td>'+v.id+'</td>'+
'<td>'+v.country_name+'</td>'+
'<td>'+v.created+'</td>'+
'<td>'+'Edit</td>'+
'</tr>';
});
$('#country_list').html(tbl_op);
$('#Response').fadeIn(1000);
$('#Response').html(data.response_msg);
$('#Response').fadeOut(8000);
</script>
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover dataTables-example" id="dataTables-example">
<thead>
<tr>
<th>Id</th>
<th>Country</th>
<th>Date</th>
<th>Action</th>
</tr>
</thead>
<tbody id='country_list'>
</tbody>
</table>
</div>
result:
<tbody id="country_list"><tr class="odd gradeX"><td>1</td><td>India</td><td>2016-09-13 11:33:30</td><td>Edit</td></tr><tr class="odd gradeX"><td>2</td><td>Usa</td><td>2016-09-13 11:33:30</td><td>Edit</td></tr><tr class="odd gradeX"><td>3</td><td>Russia</td><td>2016-09-13 11:33:30</td><td>Edit</td></tr><tr class="odd gradeX"><td>4</td><td>R1</td><td>2016-09-13 11:33:42</td><td>Edit</td></tr><tr class="odd gradeX"><td>5</td><td>R5</td><td>2016-09-13 11:34:30</td><td>Edit</td></tr><tr class="odd gradeX"><td>6</td><td>R6</td><td>2016-09-13 11:34:40</td><td>Edit</td></tr><tr class="odd gradeX"><td>7</td><td>R7</td><td>2016-09-13 11:34:48</td><td>Edit</td></tr><tr class="odd gradeX"><td>8</td><td>R8</td><td>2016-09-13 11:34:54</td><td>Edit</td></tr><tr class="odd gradeX"><td>9</td><td>R9</td><td>2016-09-13 11:35:00</td><td>Edit</td></tr><tr class="odd gradeX"><td>10</td><td>R10</td><td>2016-09-13 11:35:07</td><td>Edit</td></tr><tr class="odd gradeX"><td>11</td><td>R11</td><td>2016-09-13 11:35:13</td><td>Edit</td></tr><tr class="odd gradeX"><td>12</td><td>R12</td><td>2016-09-13 11:35:25</td><td>Edit</td></tr><tr class="odd gradeX"><td>13</td><td>R67</td><td>2016-09-13 11:37:30</td><td>Edit</td></tr></tbody>
In order to have pagination updated on XHR requests, you need to have the information about the pagination as well. That is:
Current page number
Total amount of entries
The amount of entries to show per page
This information should be returned with your XHR request. You could for example add these properties to your result object (result.currentPage, result.totalPages, result.totalEntries).
Now with each concurrent request you should send these parameters to the server, and the server should fetch the proper dataset according to these parameters.
And last but not least, with this data you can re-calculate the state of your pagination element.
Another option:
If you download the complete dataset from the server (which seems to be the case in your example), you should keep the parameters required for pagination (entriesPerPage, currentPage, totalEntries) in your JavaScript code so you can keep track of your pagination state with that and update your pagination component as such.
Please be aware that downloading all data with one XHR request could possible lead a bottleneck in your application. Imagine with a dataset with tens of thousands of records will do with your bandwidth, or even worse: with the memory consumed by JavaScript. That's why I would suggest option #1.

Angular JS ng-repeat not showing up

I was having some trouble with the Angular ng-repeat directive. The attached is my code.
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
</head>
<body ng-app="myapp">
<div ng-controller="MyController as me" >
<button ng-click="myData.doClick(item, $event)">Send AJAX Request</button>
<br/>
<tr>
<th>Test Suite Name</th>
<th>Test Suite Key</th>
<th>Date</th>
<th>Start Time</th>
<th>End Time</th>
<th>Total Tests</th>
<th>Passed</th>
<th>Failed</th>
<th>Quarantined</th>
</tr>
<tr ng-repeat="data in myData">
<th>{{data.name}}</th>
<th>{{data.plan_key}}</th>
<th>{{data.start_date}}</th>
<th>{{data.start_time}}</th>
<th>{{data.end_time}}</th>
<th>{{data.total_test}}</th>
<th>{{data.test_passed}}</th>
<th>{{data.test_failed}}</th>
<th>{{data.test_quarantined}}</th>
</tr>
<h1>Data from server: {{myData[0].name}}</h1>
</div>
<script>
var $received_data;
var test = angular.module("myapp", []);
// .config(['$httpProvider', function($httpProvider) {
// $httpProvider.defaults.useXDomain = true;
// delete $httpProvider.defaults.headers.common['X-Requested-With'];
// }
// ])
test.controller("MyController", function($scope, $http) {
$scope.myData = {};
$scope.myData.doClick = function(item, event) {
var responsePromise = $http.get("http://127.0.0.1:5000/home");
responsePromise.success(function(data, status, headers, config) {
console.log("ok")
$scope.myData = data.result;
//$scope.received_data = data.reault;
});
responsePromise.error(function(data, status, headers, config) {
alert("error?");
});
}
} );
</script>
</body>
</html>
So basically it gets a list of JSON and I want it to be printed in table form.
I am trying to do it with ng-repeat with tr ng-repeat="data in myData" but it somehow didn't show up.
However, the <h1>Data from server: {{myData[0].name}}</h1> did get printed out correctly.
I am new to AngularJS so I suppose it must be something really stupid mistake that I have made.
Thank you
I think there are a couple problems here.
1 your HTML structure should be reworked. Ideally it should look like this:
<table>
<thead>
<tr>
<th>Name</th>
<th>Start Date</th>
<th>Start Time</th>
<th>End Time</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="data in myData.result">
<td>{{data.name}}</td>
<td>{{data.start_date}}</td>
<td>{{data.start_time}}</td>
<td>{{data.end_time}}</td>
</tr>
</tbody>
</table>
2 This refers to #blowsies comment. You are using the same $scope.myData to handle the function & the data. Which is fine, but you're missing a level of nesting. Currently you have a $scope.myData.doClick() which loads the data from the server. And then you assign it at $scope.myData which is most likely having a problem. Instead, assign it to $scope.myData.result` and change your HTML accordingly.
Working example