0

I have a problem. In my company I need to try out how Angular.js works together with ASP.NET MVC. Now I want to create a simple little application. On the front page there is a view with a Kendo UI grid. Inside my App.js file I read data from my Data Controller. The Controller Action gets called but as soon as the code is done executing I get the following error:

Error Message from Visual Studio

Here is the rest of my Code:

Controller:

[HttpGet]
public JsonResult GetEmergencyRegions([DataSourceRequest]DataSourceRequest request, string searchterm)
{
    var emergencyRegions = _repository.GetEmergencyRegionBySearchTerm(searchterm);
    return Json(emergencyRegions.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
}

App.js

$scope.gridOptions = {
    columns: [{
        field: "Description",
        title: "Beschreibung"
    }, {
        field: "Region",
        title: "Region"
    }, {
        field: "Phone",
        title: "Telefon"
    }, {
        field: "HasPointOfSale",
        title: "PoS"
    }],
    pageable: true,
    dataSource: {
        pageSize: 5,
        transport: {
            read: function (e) {
                $http.jsonp('/Data/GetEmergencyRegions')
                  .then(function success(response) {
                      e.success(response.data);
                  }, function error(response) {
                      alert('something went wrong')
                      console.log(response);
                  })
            }
        }
    }
};

View with the Grid

<kendo-grid options="gridOptions">

</kendo-grid>

On Stackoverflow I already found something with adding ?callback=JSON_CALLBACK to the jsonp URL but it didn't help.

Notice

When I remove the JsonRequestBehavior.AllowGet inside my Controller, I don't get the error but then I get the status Code 404.

Keren Caelen
  • 1,466
  • 3
  • 17
  • 38
brothers28
  • 1,196
  • 17
  • 23

2 Answers2

0

It seem you redundant "," in App.js

Change

 columns: [{
    field: "Description",
    title: "Beschreibung",
},

to

 columns: [{
    field: "Description",
    title: "Beschreibung"
},

Hope it help.

Lewis Hai
  • 1,114
  • 10
  • 22
0

Found the solution. There were several things to change:

  1. Remove DataSourceRequest part in Controller. With this change the critical JavaScript error disapears.

    public JsonResult GetEmergencyRegions(string searchterm)
    {
        var emergencyRegions = _repository.GetEmergencyRegionBySearchTerm(searchterm);
        return Json(emergencyRegions, JsonRequestBehavior.AllowGet);
    }
    
  2. I now had a 404 error again in my App.js. Solution for that is changing jsonp to get. I found this solution in the following link: AngularJS: how to make a jsonp request

    read: function (e) {
         $http.get('/Data/GetEmergencyRegions?callback=JSON_CALLBACK')
             .then(function success(response) {
                  e.success(response.data);
              }, function error(response) {
                  alert('something went wrong')
                  console.log(response);
         })
    }
    
  3. Run the Solution and be Happy :-)

Community
  • 1
  • 1
brothers28
  • 1,196
  • 17
  • 23