【问题标题】:How to insert data in eXist-db by $http in AngularJS through the REST API?如何通过 REST API 在 AngularJS 中通过 $http 在 eXist-db 中插入数据?
【发布时间】:2015-12-19 02:43:07
【问题描述】:

我是 AngularJS 的新手。现在我尝试通过 AngularJS 的 REST API 在 eXist-db 数据库中插入 xml 数据。我的文档包含这样的内容:

<students>
 <student>
      <id>1</id>
      <name>one</name>
 </student>
 <student>
      <id>2</id>
      <name>two</name>
 </student>
</students>

我想在两个学生之后插入一个学生,如下所示:

<students>
 <student>
      <id>1</id>
      <name>one</name>
 </student>
 <student>
      <id>2</id>
      <name>two</name>
 </student>
 <student>
      <id>3</id>
      <name>three</name>
 </student>
</students>

我在 Postman 客户端中使用 POST 请求进行测试,它的工作原理如下图所示: URL is http://localhost:9999/exist/rest/db/data/studentdata.xml, Content-Type is application/x-www-form-urlencoded

之后我尝试像这样在 AngularJS 中使用 $http:

$scope.saveData = function(){
    var req={
    method:'POST',
    url:'http://localhost:9999/exist/rest/db/data/studentdata.xml',
    header:{
        'Authorization':'Basic YWRtaW46MTIzNDU2',
        'Content-Type':'application/x-www-form-urlencoded'
    },
    data:{_query:'update insert <student><id>3</id><name>three</name></student> into //students'}   
     };

     $http(req).then(function(response){
         alert("save finish");
     });
 }//end function

但它有错误 400(解析请求时出现 SAX 异常:prolog 中不允许内容)。

你能告诉我是什么问题吗?我该如何解决这个错误?非常感谢。

【问题讨论】:

    标签: angularjs rest


    【解决方案1】:

    现在我可以用这段代码运行它了(没有任何错误)。

    var promise = $http({
             method:'POST', 
             url: 'http://localhost:9999/exist/rest/db/data/studentdata.xml',
             headers: {'Authorization': 'Basic YWRtaW46MTIzNDU2','Content-Type':'application/x-www-form-urlencoded'},
             data:'_query=update insert <student><id>3</id><name>three</name></student> into //students'
        });
         promise.success(function(data){
             alert("successful");
         });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-02
      • 1970-01-01
      • 2012-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      相关资源
      最近更新 更多