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

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

From Dev

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

From Dev

Variable Scope in Nested AJAX Calls

From Dev

Angular JSON using $scope data as key value

From Dev

angular scope variable not binding

From Dev

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

From Dev

Ajax : empty json response when using Karma

From Dev

Json response to ajax

From Dev

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

From Dev

Using variable from ajax response as json

From Dev

Angular $scope variable not updating

From Dev

symfony Response with json ajax

From Dev

Setting Ajax response to drop down using json

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

Using Ajax to pass Json variable and decode

From Dev

Display json response in grid using angular js

From Dev

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

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

Angular not working in an ajax response

From Dev

Using angular scope directive variable content as an attribute itself

From Dev

Display the JSON response received using AJAX

From Dev

How to execute javascript/html using angular scope variable

From Dev

angular js success response $scope variable another function

From Dev

assigning GET response to the scope variable

From Dev

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

From Dev

Extract json response to shell variable using jq

From Dev

Ajax response using json

Related Related

  1. 1

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

  2. 2

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

  3. 3

    Variable Scope in Nested AJAX Calls

  4. 4

    Angular JSON using $scope data as key value

  5. 5

    angular scope variable not binding

  6. 6

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

  7. 7

    Ajax : empty json response when using Karma

  8. 8

    Json response to ajax

  9. 9

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

  10. 10

    Using variable from ajax response as json

  11. 11

    Angular $scope variable not updating

  12. 12

    symfony Response with json ajax

  13. 13

    Setting Ajax response to drop down using json

  14. 14

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

  15. 15

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

  16. 16

    Using Ajax to pass Json variable and decode

  17. 17

    Display json response in grid using angular js

  18. 18

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

  19. 19

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

  20. 20

    Ajax callback and variable scope when loading JSON file into array

  21. 21

    Angular not working in an ajax response

  22. 22

    Using angular scope directive variable content as an attribute itself

  23. 23

    Display the JSON response received using AJAX

  24. 24

    How to execute javascript/html using angular scope variable

  25. 25

    angular js success response $scope variable another function

  26. 26

    assigning GET response to the scope variable

  27. 27

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

  28. 28

    Extract json response to shell variable using jq

  29. 29

    Ajax response using json

HotTag

Archive