简体   繁体   English

如何通过REST API在AngularJS中通过$ http在eXist-db中插入数据?

[英]How to insert data in eXist-db by $http in AngularJS through the REST API?

I am new in AngularJS. 我是AngularJS的新手。 Now I try to insert xml data in eXist-db Database through the REST API from AngularJS. 现在,我尝试通过AngularJS的REST API在eXist-db数据库中插入xml数据。 My document contains something like this: 我的文档包含以下内容:

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

I want to insert one student after the two students like this: 我想在两个学生之后插入一个学生,如下所示:

<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>

I test with POST request in Postman client and it works as following picture: URL is http://localhost:9999/exist/rest/db/data/studentdata.xml, Content-Type is application/x-www-form-urlencoded 我在Postman客户端中使用POST请求进行测试,其工作原理如下图: URL为http:// localhost:9999 / exist / rest / db / data / studentdata.xml,Content-Type为application / x-www-form-urlencoded

After that I try to use $http in AngularJS like this: 之后,我尝试像这样在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

But it has error 400 (SAX exception while parsing request: Content is not allowed in prolog). 但是它有错误400(解析请求时出现SAX异常:序言中不允许内容)。

Could you please tell me what is the problem? 你能告诉我是什么问题吗? How can I solve this error? 我该如何解决这个错误? Thank you very much. 非常感谢你。

Now I can run it already with this code (without any error). 现在,我可以使用此代码运行它了(没有任何错误)。

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");
     });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM