angularjs - 成功功能无法正常工作

标签 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;
})

更新

我已经用 then 替换了 .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;

最佳答案

似乎 successerror 已被弃用,您应该改用 then:

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

The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error. Source here

关于angularjs - 成功功能无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32589171/

相关文章:

javascript - 如何让后退按钮与 AngularJS ui-router 状态机一起使用?

Javascript、AngularJS 和 setTimeout 函数

Angular 子路由加载父组件而不是子组件

javascript - 路由器解析不会注入(inject) Controller

javascript - UI-Router过滤Json数据

angular - Ionic 4 - Angular 路由器

使用 CORS 的 Javascript 和 angularjs

javascript - 带有 Ng-If on Angular 指令的 jQuery Lite 元素查找器

AngularJs 可调整大小的边框布局

javascript - Angular Js/ui-路由器/选项卡设置, Controller 被调用两次