简体   繁体   中英

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

I am new in AngularJS. Now I try to insert xml data in eXist-db Database through the REST API from AngularJS. 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

After that I try to use $http in AngularJS like this:

$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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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