How to pass json data from C# controller to angular js?

Kavitha

How to pass json data from C# controller to angular js ? I want to pass json from controller to angular js. I tried different but none of them worked. See my code below,

var app = angular.module('myApp', []);
app.controller('customersCtrl', function ($scope, $http) {
    $http.get("/adnActions/getJson")
    .success(function (response) { alert(response.records); $scope.names = response.records; });
});

C# controller code

 public async Task<string> getJson()
    {
                 data="{'records':[ {'Name':'Alfreds Futterkiste','City':'Berlin','Country':'Germany'}, {'Name':'Ana Trujillo Emparedados y helados','City':'México D.F.','Country':'Mexico'}]}";
       return data;    

    }

but am unable to get the data in angular js controller below is the error raised in console, "Error: JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data

how to fix this? Whats the problem here?

kmcnamee

I suspect it is because the format of your JSON. You are using single quotes vs double. Valid JSON uses double quotes.

Here is what i get when i run your json i have changed the response to be HttpResponseMessage and explicitly set the response content type to eliminate this as an issue. Based on your error being on Javascript side i think your problem is your single quotes in your JSON.

    public async Task<HttpResponseMessage> GetJsonSingle()
    {
        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new StringContent("{'records':[ {'Name':'Alfreds Futterkiste','City':'Berlin','Country':'Germany'}, {'Name':'Ana Trujillo Emparedados y helados','City':'México D.F.','Country':'Mexico'}]}")
        };

        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

        return result;
        }

results in:

busted

While double quotes:

     public async Task<HttpResponseMessage> GetJsonDouble()
    {
        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new StringContent("{\"records\":[ {\"Name\":\"Alfreds Futterkiste\",\"City\":\"Berlin\",\"Country\":\"Germany\"}, {\"Name\":\"Ana Trujillo Emparedados y helados\",\"City\":\"México D.F.\",\"Country\":\"Mexico\"}]}")
        };

        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

        return result;
    }

works correctly:

correct

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Magento How to pass data from a controller to a template?

From Dev

How to pass async data from directive to controller?

From Dev

How to pass raw data from controller into javascript?

From Dev

Pass RequestVerificationToken from Angular js to mvc controller

From Dev

How to pass data from factory to controller in angular js?

From Dev

How to pass data from one page to other page in angular js

From Dev

How Do I call and pass data to a Directive from a Controller Angular

From Dev

In angular js, How do I pass data from a parent controller to a child controller?

From Dev

How to pass data from a form to controller in CakePHP?

From Dev

How to pass data from a custom angular element to the controller?

From Dev

Angular: pass data from service/factory to controller

From Dev

How should I get the data from JSON into another angular js controller?

From Dev

How to pass data from View controller to Container?

From Dev

pass data to angular controller from a global function

From Dev

How to Pass data from directive template to controller?

From Dev

Pass data from an Angular modal's controller back to the main controller

From Dev

How to correctly pass data from Service to Controller

From Dev

How to pass variables from angular controller to jquery

From Dev

pass json data from controller to view

From Dev

How to pass data from one page to other page in angular js

From Dev

Angular: pass data from service/factory to controller

From Dev

pass data to angular controller from a global function

From Dev

Pass data from angular form to another controller

From Dev

how to pass data from service to directive in angular.js

From Dev

How to pass data from controller.js to map.js?

From Dev

Pass Json data from angular JS to spring REST API and handling it

From Dev

How to pass json data through ajax in Angular Js

From Dev

How to get data from spring controller to angular js

From Dev

how to pass json data to controller in aspnetcore 2

Related Related

  1. 1

    Magento How to pass data from a controller to a template?

  2. 2

    How to pass async data from directive to controller?

  3. 3

    How to pass raw data from controller into javascript?

  4. 4

    Pass RequestVerificationToken from Angular js to mvc controller

  5. 5

    How to pass data from factory to controller in angular js?

  6. 6

    How to pass data from one page to other page in angular js

  7. 7

    How Do I call and pass data to a Directive from a Controller Angular

  8. 8

    In angular js, How do I pass data from a parent controller to a child controller?

  9. 9

    How to pass data from a form to controller in CakePHP?

  10. 10

    How to pass data from a custom angular element to the controller?

  11. 11

    Angular: pass data from service/factory to controller

  12. 12

    How should I get the data from JSON into another angular js controller?

  13. 13

    How to pass data from View controller to Container?

  14. 14

    pass data to angular controller from a global function

  15. 15

    How to Pass data from directive template to controller?

  16. 16

    Pass data from an Angular modal's controller back to the main controller

  17. 17

    How to correctly pass data from Service to Controller

  18. 18

    How to pass variables from angular controller to jquery

  19. 19

    pass json data from controller to view

  20. 20

    How to pass data from one page to other page in angular js

  21. 21

    Angular: pass data from service/factory to controller

  22. 22

    pass data to angular controller from a global function

  23. 23

    Pass data from angular form to another controller

  24. 24

    how to pass data from service to directive in angular.js

  25. 25

    How to pass data from controller.js to map.js?

  26. 26

    Pass Json data from angular JS to spring REST API and handling it

  27. 27

    How to pass json data through ajax in Angular Js

  28. 28

    How to get data from spring controller to angular js

  29. 29

    how to pass json data to controller in aspnetcore 2

HotTag

Archive