Okay so I'm connecting to a freshdesk API to get what tickets are open. Now I want to display this dynamically in my own web app using angular.js. At the moment I am receiving a json response from the freshdesk API but I cannot store it into a variable to then parse into my angular controller $scope.tickets variable.
How can I do this, is there a better way? I am quite new to angular and jQuery so open to any suggestions on how I can do this.
This is my code :
var jsonData;
function getData() {
$.support.cors = true;
var settings = {
"async" : true,
"crossDomain" : true,
"url" : "https://helpdesk.example.com/helpdesk/tickets.json",
"type" : "GET",
"headers" : { "authorization": basicAuth, "Content-Type" : "application/json"}
}
$.ajax(settings).done(function (response) {
//alert(response);
console.log(response);
jsonData = response;
});
}
I have tried jsonData = JSON.parse(response); But this doesn't work and it is a valid json response.
My angular controller:
app.controller('Ctrl', function ($scope, $http) {
$scope.tickets = jsonData;
});
You don't have to parse the json response. example, if your response look like this.
[
{"ticket_id":1,"desc":"some desc"},
{"ticket_id":2,"desc":"some issues"}
]
then you can directly use it as
var tickets= response.data;
console.log(tickets[0].desc);
you can than directly set the scope variable $scope.tickets. Also you should use http service of angular for making http calls rather than ajax.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments