【问题标题】:insert array to moongose using angular and node使用角度和节点将数组插入猫鼬
【发布时间】:2017-08-14 19:53:00
【问题描述】:

我在使用 mlab(mongoose)、angular.js、html、javascript 时遇到问题。 当我将带有值的数组从 angular.js 控制器传递到服务器端(node.js) 它不起作用..它不会将数据插入到我在 mlab 中的架构中

这是我的 html 代码:

<form enctype="multipart/form-data" method="POST" action="http://localhost:3000/addPersonal">
    <tags-input ng-model="Tags" name="Tags"></tags-input>
          <button type="submit" class="btn btn-default" ng-click="clickme()">Submit</button>
 </form>

我在 angular.js 中的控制器(客户端):

mymedical.controller('insertPersonalCtrl',

    ['$scope','$http',function($scope,$http){   

            $scope.clickme = function(){  
                    var data = {};      
                    data.Tags = $scope.Tags;     
                $http.post('http://localhost:3000/addPersonal', data).then()
            }                
    }]);

我在服务器端的控制器(node.js):

 var savePersonal = new personal({
                Tags:request.body.Tags
              });
          savePersonal.save(function(error, result) {}

架构:

var privateSchema = new schema({
  Tags : []
},{collection: 'personal'});

我需要做什么? 谢谢,

【问题讨论】:

  • 能否请您发布错误日志?
  • 从您的代码中我可以看到您没有检查错误。请修改以记录错误。
  • XMLHttpRequest 无法加载 localhost:3000/addPersonal。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问 Origin 'localhost:8080'。
  • @adi 所以你不设置节点服务器的cors
  • @KxngKombian 感谢您的回答!你能解释一下我需要做什么吗?

标签: javascript angularjs arrays node.js


【解决方案1】:

所以你需要在nodejs服务器上设置一个cors中间件。

CORS 允许 Web 脚本更开放地与原始域之外的内容进行交互。

一个好的包是https://github.com/expressjs/cors

【讨论】:

    猜你喜欢
    • 2019-10-26
    • 1970-01-01
    • 2015-05-14
    • 2017-02-25
    • 2022-01-07
    • 2021-01-30
    • 2019-02-04
    • 2020-06-03
    • 2015-01-27
    相关资源
    最近更新 更多