view raw
WalidRoamsmart WalidRoamsmart - 6 months ago 30
reST (reStructuredText) Question

restful angularjs $http get return null

I'm trying AngularJS for the first time. I'm getting as imple user JSON data from a http-get request, but the object is returned null. I tested my service using chrome and I get this result:


Below my js file:

var app = angular.module("userApp", []);
app.controller("userController", function($scope, $http) {
$scope.user = null;
$ = null;

$scope.getUser = function() {
method : 'GET',
url : '/all',
}).success(function(data) {
$scope.user = json.stringify(data);
}).error(function(data) {
alert('Fail: user is '+data);



<!DOCTYPE html>
<meta charset="UTF-8">
<title>SIM Card</title>
<link rel="Stylesheet" type="text/css"
<link rel="Stylesheet" type="text/css" href="css/simcard.css">
<script type="text/javascript" src="angular/angular.min.js"></script>
<script type="text/javascript" src="js/simcard2.js"></script>
<script type="text/javascript" src="js/json2.js"></script>
<body ng-app="userApp" ng-controller="userController">

<div class="col-md-12 col-sm-12 col-xs-12">
<div class="panel panel-info spacer">
<div class="panel-heading">User Management</div>
<div class="panel-body">
<form action="">
<div class="form-group">
<label>ID</label> <input type="text" ng-model="email">
<button ng-click="getUser()" type="submit">Save</button>
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="panel panel-info spacer">
<div class="panel-heading">Result</div>
<div class="panel-body">
<div class="form-group">
<label>Name: </label>

I can send the whole maven project in case needed. Thank you for your help.



The problem is in your config of the request:

It doesn't know what type of data you expect in response.

Your http request should look like this:

 $http.get("test.json").success(function(data) {
    $scope.user = data[0];
}).error(function(data) {
    alert('Fail: user is 2 '+data);


Just noticed something in yout HTML - you have there a form with action empty. This is the cause of the request being canceled. in angularjs to have a form, it is enought to add to a div ng-form="myForm" and the input can be just simple button, not submit and it is going to work fine.