Using ajax json response in an angular scope variable

Jason

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;
});
Sugam

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using variable from ajax response as json

From Dev

Ajax response using json

From Dev

angular js success response $scope variable another function

From Dev

Ajax : empty json response when using Karma

From Dev

Setting Ajax response to drop down using json

From Dev

Display the JSON response received using AJAX

From Dev

Angular JSON using $scope data as key value

From Dev

Angular updating $scope variable after ajax is not reflecting in UI

From Dev

Angular updating $scope variable after ajax is not reflecting in UI

From Dev

Ajax callback and variable scope when loading JSON file into array

From Dev

Extract json response to shell variable using jq

From Dev

assigning GET response to the scope variable

From Dev

could not update a variable in an angular service with ajax response data and use is to filter

From Dev

could not update a variable in an angular service with ajax response data and use is to filter

From Dev

Display json response in grid using angular js

From Dev

Issues in storing a portion of an AJAX GET response (in JSON) into a string variable

From Dev

angular scope variable not binding

From Dev

Angular $scope variable not updating

From Dev

How to get the ajax response from success and assign it in a variable using jQuery?

From Dev

How to get the ajax response from success and assign it in a variable using jQuery?

From Dev

How to get response text from ajax callback in a variable using ExtJS

From Dev

How to assign class to an element using $scope variable in angular js

From Dev

Using angular scope directive variable content as an attribute itself

From Dev

How to execute javascript/html using angular scope variable

From Dev

Angular not working in an ajax response

From Dev

Using Ajax to pass Json variable and decode

From Dev

Variable Scope in Nested AJAX Calls

From Dev

Json response to ajax

From Dev

symfony Response with json ajax

Related Related

  1. 1

    Using variable from ajax response as json

  2. 2

    Ajax response using json

  3. 3

    angular js success response $scope variable another function

  4. 4

    Ajax : empty json response when using Karma

  5. 5

    Setting Ajax response to drop down using json

  6. 6

    Display the JSON response received using AJAX

  7. 7

    Angular JSON using $scope data as key value

  8. 8

    Angular updating $scope variable after ajax is not reflecting in UI

  9. 9

    Angular updating $scope variable after ajax is not reflecting in UI

  10. 10

    Ajax callback and variable scope when loading JSON file into array

  11. 11

    Extract json response to shell variable using jq

  12. 12

    assigning GET response to the scope variable

  13. 13

    could not update a variable in an angular service with ajax response data and use is to filter

  14. 14

    could not update a variable in an angular service with ajax response data and use is to filter

  15. 15

    Display json response in grid using angular js

  16. 16

    Issues in storing a portion of an AJAX GET response (in JSON) into a string variable

  17. 17

    angular scope variable not binding

  18. 18

    Angular $scope variable not updating

  19. 19

    How to get the ajax response from success and assign it in a variable using jQuery?

  20. 20

    How to get the ajax response from success and assign it in a variable using jQuery?

  21. 21

    How to get response text from ajax callback in a variable using ExtJS

  22. 22

    How to assign class to an element using $scope variable in angular js

  23. 23

    Using angular scope directive variable content as an attribute itself

  24. 24

    How to execute javascript/html using angular scope variable

  25. 25

    Angular not working in an ajax response

  26. 26

    Using Ajax to pass Json variable and decode

  27. 27

    Variable Scope in Nested AJAX Calls

  28. 28

    Json response to ajax

  29. 29

    symfony Response with json ajax

HotTag

Archive