2017-06-07 20 views
0

因此,我为户外创建了一个自定义共享页面,并且我想使用javascript创建一个人,感谢REST API(您会发现所有关于api的文档以及为什么我塑造了我的VAR这里这样),所以我写了这个代码,其中包含一个XMLHttpRequest发布的数据:使用xmlhttprequest创建一个使用户外RESTApi的人

var boby = "{ \"id\": \"bob\",\"firstName\": \"Bob\", \"lastName\": \"LeBricoleur\", \"email\": \"[email protected].com\", \"telephone\": \"6666666666\", \"enabled\": true,\"emailNotificationsEnabled\": true, \"password\": \"bob\" }"; 
         console.log(boby); 
         function loadDoc() { 
          var xhttp = new XMLHttpRequest(); 
          xhttp.onreadystatechange = function() { 
          if (this.readyState == 4 && this.status == 200) { 
           document.getElementById("creatingperson").innerHTML = this.responseText; 
          } 
          }; 
          xhttp.open("POST", 'http://localhost:8081/share/proxy/alfresco-api/-default-/public/alfresco/versions/1/people', true); 
          xhttp.setRequestHeader("Content-Type", "application/json"); //curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'Authorization: Basic YWRtaW46YWRtaW4=' -d '[...] 
          xhttp.send(boby); 
         } 

好了,所以这并没有给我什么..甚至不是一个错误消息。起初我以为这是因为我的整个代码,所以我创建了变量boby,我将发送到服务器以简化调试,但我仍然不知道错误来自哪里。

这是我的代码问题吗?从我使用API​​的方式?从露天份额本身?如果你能帮助我,谢谢!

PS:我写了使用相同的URL,其工作的代码列表(而不是创建一个),如果这能帮助


[编辑]

我找到了另一种方式来让我的要求和它的作品,但我不知道为什么..这里是代码:

var createperson = "{ \"id\": \"" + document.getElementById('login').value + "\",\"firstName\": \"" + document.getElementById('firstName').value + "\", \"lastName\": \"" + document.getElementById('lastName').value + "\", \"email\": \"" + document.getElementById('mail').value + "\", \"telephone\": \"" + document.getElementById('telephone').value + "\", \"enabled\": true,\"emailNotificationsEnabled\": true, \"password\": \"" + document.getElementById('pwd2').value + "\" }"; 
         console.log(createperson); 
          var url = "http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/people"; 
          var method = "POST"; 
          var async = true; 
          //var postData = "{ \"id\": \"bob\",\"firstName\": \"Bob\", \"lastName\": \"LeBricoleur\", \"email\": \"[email protected]\", \"telephone\": \"6666666666\", \"enabled\": true,\"emailNotificationsEnabled\": true, \"password\": \"bob\" }"; 

          var request = new XMLHttpRequest(); 
          request.onload = function() { 
           var status = request.status; // HTTP response status, e.g., 201 for "201 OK" 
           var data = request.responseText; // Returned data, e.g., an HTML document. 
           console.log("status :"); 
           console.log(status); 
           console.log("data :"); 
           console.log(data); 
          } 

          request.open(method, url, async); 
          request.setRequestHeader("Content-Type", "application/json"); 
          request.setRequestHeader("Authorization", "Basic YWRtaW46YWRtaW4="); 
          request.send(createperson); 

而且当我尝试使用这种类型的请求,要求列人(只是到处使用的方法),它不在这里工作了..是我写的,列出一部分人:

var url = "http://192.168.1.103:8080/alfresco/api/-default-/public/alfresco/versions/1/people"; 
     var method = "GET"; 
     var async = true; 
     var request = new XMLHttpRequest(); 
     request.onload = function() { 
       var status = request.status; // HTTP response status, e.g., 201 for "201 OK" 
      objet = JSON.parse(request.responseText); 
       console.log("status :"); 
       console.log(status); 
       console.log("data :"); 
       console.log(objet); 
     } 

     request.open(method, url, async); //, "admin", "admin"); 
     request.setRequestHeader("Content-Type", "application/json"); 
     request.setRequestHeader("Authorization", "Basic YWRtaW46YWRtaW4="); 

因此,如果任何人都知道为什么,它的工作创造一个人,为什么它不上市感谢您的回答!

+0

为什么不使用某种REST客户端重新创建有问题的API请求并确保它在那里工作?这样,你就会确定那部分工作正常。 – Lista

+0

你正在使用哪个版本的露天场景? –

+0

@Lista这部分作品我已经做到了,这要感谢邮递员,我得到了一个好结果 – JackA

回答

1

基本上可以存在2个问题。

1.此API只支持在露天版本5.2和字母中,并且您的版本应低于5.2。 2. xhr调用中存在一些CSRF令牌问题,可以通过添加csrf令牌来解决。对于该功能代码如下。

function loadDoc() { 
          var xhttp = new XMLHttpRequest(); 
          xhttp.onreadystatechange = function(demo) {console.log(demo.error); 
          if (this.readyState == 4 && this.status == 200) { 
           console.log(this); 
          } 
          }; 
          xhttp.open("POST", 'http://localhost:8081/share/proxy/alfresco-api/-default-/public/alfresco/versions/1/people', true); 
          xhttp.setRequestHeader("Content-Type", "application/json"); 
          xhttp.setRequestHeader(Alfresco.util.CSRFPolicy.getHeader(), Alfresco.util.CSRFPolicy.getToken());//CSRF Token 
          xhttp.send(boby); 
         } 
+0

我已经改变了我提出请求的方式,现在它可以工作,我不知道为什么,但它确实有效。我会更新我的原始帖子,让你看到新的代码。但是,当我尝试使用相同的方法时,这次列出其他人不起作用。 – JackA

+0

对于列表人员,请求方法为get而不是post。 –

+0

对不起,翻译错误,当我说方法我的意思类型的http请求,你可以看到上面,我用GET – JackA