How to delete data from mysql database with angularjs $http request? - javascript

Here is the code
$scope.Delete = function(customer ){
$http({
method: 'get',
url: 'rest/customers',
data: {customers:customer.custId},
}).then(function successCallback(response) {
$scope.customers.splice($scope.customers.indexOf(customer), 1);
});
}
If I call this function it only deletes the data from the front-end. The data is not getting deleted from mysql table. Can anyone help me with this.

//try like this..sure it will work
$http.delete('your url' + id).then(function(result){
if(result.data){
//your handle code
}
}, function(err) {
console.log(err);
});

First, it is not efficient to test api with front end code, you need to set up a postman test to separate your concern, get it working in postman first.
After postman test, you can use $http.delete, sample link is here

Related

rest api authorize using javascript

I was trying to create a api web data connector for tableau, but stuck in the authorization phase. I have a header key and value (e.g. api_header, value123) needs to pass using javascript.
// Download the data
myConnector.getData = function(table, doneCallback) {
$.getJSON("https://testapi/data",
function(resp) {
var tableData = [];
How to pass the key and value here?
I'm not sure .getJSON supports headers, at least the documentation doesn't mention them.
According to this answer, you could use .ajax instead like so:
$.ajax({
url: 'https://testapi/data',
headers: { api_header: 'value123' }
});

How to pull data from JIRA using JS?

I want to pull the Data from my JIRA account using JS, as I want to automate a thing i.e. Time Tracking.
It will track the time whether the existing resources have sufficient time/bandwidth to deliver the upcoming JIRA Tickets or not.
Kindly Help!!
You should use the REST-API of JIRA in combination with angular http client. This is one of the first tutorials you already should have done with angular.
Edit Because of Comments:
JIRA REST API and JQL should work. it should look like this:
$http({
method: "GET",
url: 'http://my-url/rest/api/2/search?jql=' +
'reporter=reporterXY' +
'&project=projectXY'
}).then(function successCallback(response) {
return response.data;
}, function errorCallback() {
console.log("Error calling API")
});

Retrieving Json Object from action method in ASP.NET

I have searched and seen many solutions but yet I still am not getting any progress. I get no errors within the console and get nothing in return from the ajax request.
Here is action method code:
[HttpGet]
public ActionResult GetEmployees()
{
AWEmployeeModel aw = new AWEmployeeModel();
aw.ID = 0;
aw.Name = "Selena";
aw.Position = "Cashier";
aw.Department = "Finance";
return Json(aw,JsonRequestBehavior.AllowGet);
}
And here is my ajax call within my controller:
EmpApp.controller("AWEmpControl", function ($scope) {
loadEmployees();
function loadEmployees() {
$.ajax({
url: "/AWEmployee/Index/GetEmployees",
method: "GET",
success:function (response) {
$scope.employees = response["Name"];
}
});
}
});
I do also have the ng directives on the desired page:
<div class="container" ng-app="AWEmpApp" ng-controller="AWEmpControl">
{{employees}}
</div>
I tested to see if the scripts work fine by assigning $scope.employees = "Hello..."; and no problems, so I know it has nothing to do with script loading.... What could be the issue? I've made sure to describe the request as GET and to return a JSON object. Am I missing something?
Thanks in advance.
EDIT
I checked the firefox console and got back Parsing Error: No XML found at location.......
Which I found odd since the server returns a JSON object, so I replaced the ajax method with the following:
function loadEmployees() {
$http.get('/AWEmployee/Index/GetEmployees')
.then(function(response) {
$scope.employees =response;
});
};
Now response is html data of the page that called it (Index), instead of the JSON object ! I assume something is happening in between the communication as suggested in this post.
As you use JQuery to fetch data from server instead of $http service and moreover success function is executed asynchronous, AngularJS will not reflect any changes that was happened, to force it, just wrap assigning into $apply:
function loadEmployees() {
$.ajax({
url: "/AWEmployee/Index/GetEmployees",
method: "GET",
success:function (response) {
$scope.$apply(function(){
$scope.employees = response["Name"];
});
}
});
}
Found out the issue, it's a simple mistake on my part. I was calling http://localhost:53729/AWEmployees/Index/GetEmployees which was the wrong thing to do. I took out the Index page, so now the url looks like http://localhost:53729/AWEmployees/GetEmployees and it worked perfectly!

Cannot retrieve JSON data from Last.FM's API

I am working on a school project and have run into trouble. Namely, I am building a simple AngularJS app which shows some kind of a radio chart based on Last.FM's data.
However, when trying to access the API, I never get a response.
This is the code that I use for accessing:
APIservice.getChartTracks = function () {
return $http({
method: 'JSONP',
url: 'http://ws.audioscrobbler.com/2.0/?method=chart.gettoptracks&api_key=MY_API_KEY&format=json'
});
}
Afterwards, when I do something like this:
APIservice.getChartTracks().success(function (response) {
$scope.tracks = response.tracks.track;
});
the success() method never gets called. If I were to change the url to this one (found it on some online tutorial), I get a response and everything.
How should I go about accessing Last.FM's API?
Thanks in advance.
With $http, the method to use is GET, not JSONP. Write:
return $http({
method: 'GET',
url: 'http://ws.audioscrobbler.com/2.0/?method=chart.gettoptracks&api_key=MY_API_KEY&format=json'
});
Or use the shortcut:
return $http.get('http://ws.audioscrobbler.com/2.0/?method=chart.gettoptracks&api_key=MY_API_KEY&format=json');

Trouble sending AJAX GET request to mongodb

I am currently trying to display data from a mongodb collection on the browser using nodejs and express and am having a very tough time with it. Here is the call itself on the clientside.
document.onload= $(function (e) {
var i = 0;
$.ajax({
type: "GET",
url: "http://localhost:3000/viewdata",
dataType: "jsonp",
jsonp: 'jsonp',
success: function (responseData, status) {
//code doing things with the data
}
Here is whats going on in node.
app.post('/viewdata', function(req, res){
tweets.find({}, function (err, doc) {
res.render('../../views/view.html', doc);
});
});
The call is returning "200 OK". I was able to view the data from mongo on the console so I know the data is there, I am not sure how to get to it and display it on the browser though. Thanks for the help
For anyone looking for the answer to this question, the problem is that I am trying to get data from the server in a post request. I made a separate GET request below the POST request and sent the data to the view. You can now use an ajax get request and use the data on the client-side.
app.get('/viewdata', function(req, res){
tweets.find().toArray(function(err, items) {
/*console.log(items);*/
res.send(items);
});
});
And on the clientside:
$.get("/viewdata", function(data) {
/*do stuff with the data*/
}

Categories