成功功能不起作用

时间:2015-09-15 14:52:17

标签: angularjs angular-ui-router

我正在发送http请求,当该请求完成时,我正试图进入另一个状态,但问题是它没有进入成功回调我认为我正在收到错误所以我写了错误回调它不适合。任何人都可以告诉我我做错了什么

  $scope.submitUpdatedData= function(user){
    debugger;
    // $http.post('/url',{params: value}).sucess(function(){
    API.updateRecord(user).success(function(res){
      $state.go('app' ,{} , {reload: true });
      console.log("Hello");
    });
  }

API代码如下。在这里我调用http调用

.factory('API', function($http) {
  var api = {};
  var baseURL = 'http://localhost:3000';

  api.addRecord = function(record) {
      console.log(record);
    // $http.post(baseURL + '/addData', {name: record.name}.);
      return $http.post(baseURL + '/addData', {rec:record});
  };

  api.deleteRecord = function(id){
    return $http.get(baseURL +'/delete/' + id );
  };

  api.updateRecord  = function(user){
    return $http.post(baseURL + "/update/" ,{rec:user});
  };

  api.getAllRecord = function(){
    return $http.get(baseURL+'/getAll');
  };

  api.getOneRecord = function(id){
    return $http.get(baseURL + '/getOne/' + id)
  };

  return api;
})

更新

我已经用。替换了.success部分,但它仍无法正常工作

第二次更新

这是我的服务器端代码

  var express = require('express');
var mongoose = require('mongoose');
var util = require('util');
var bodyParser = require('body-parser')
var app = express();
var Schema = mongoose.Schema;
require('node-monkey').start({host: "127.0.0.1", port:"50500"});

var allowCrossDomain = function(req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
  res.header('Access-Control-Allow-Headers', 'Content-Type');

  next();
};
app.use( bodyParser.json() );
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: true
}));
// app.use(express.json());       // to support JSON-encoded bodies
// app.use(express.urlencoded()); // to support URL-encoded bodies
app.use(allowCrossDomain);
// app.use('/' , require('./index'))
mongoose.connect('mongodb://localhost:27017/myappdatabase');

var userSchema = new Schema({
  name:  String,
  password: String
});



var Todo = mongoose.model('Todo', userSchema);


app.get('/getAll' , function(req, res){
    Todo.find({} , function(err , todos){
      if (err){
        res.send(err);
      }
      console.log(todos);
      res.send(todos);
    });
});

app.get('/delete/:name' , function(req , res){
  console.log(req.params);
  console.log(req.params.name);
  Todo.remove({
            name : req.params.name
        }, function(err, todo) {
            if (err)
                res.send(err);
            // get and return all the todos after you create another
            Todo.find(function(err, todos) {
                if (err)
                    res.send(err)
                res.json(todos);
            });
        });
});

app.get('/getOne/:id' , function(req , res){
  Todo.find({name : req.params.id}, function(err, todo) {
            if (err)
                res.send(err);
            res.send  (todo[0]);
            // get and return all the todos after you create another
        });
});


app.post('/update', function(req , res){
  console.log(req.param('rec').name);
  Todo.update({_id:req.param('rec').id} , {$set : {name:req.param('rec').name , password:req.param('rec').password}} , function(err){
    if(err)
      res.send("Error occured");
    res.send("true");
  });
});


app.post('/addData' , function(req , res){
  console.log( req.param('rec').name);

  var p = new Todo({name: req.param('rec').name  , password: req.param('rec').password});
  p.save(function(err){
    if(err){
      res.send(err);
      console.log(error);
    }
    res.json(p);
  });
});


var server = app.listen(3000, function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);
});



// module.exports = app;

2 个答案:

答案 0 :(得分:3)

似乎不推荐使用successerror,您应该使用then代替:

API.updateRecord(user).then(function(res){
    $state.go('app' ,{} , {reload: true });
    console.log("Hello");
});
  

$ http遗留承诺方法成功与错误   弃用。请改用标准方法。如果   $ httpProvider.useLegacyPromiseExtensions设置为false然后这些   方法将抛出$ http / legacy错误。   来源here

答案 1 :(得分:0)

似乎您的请求永远不会被API服务器回答。也许您可以为您的请求设置超时。 Here它说你可以这样做:

$http.post(url, data, {timeout: 100});

这应该会在100毫秒后超时您的请求。