2017-06-07 13 views
0

私はalfrescoのカスタム共有ページを作成しています。REST APIのおかげでjavascriptを使って人を作成したいと思います。xmlhttprequestを使ってalfresco RESTApiを作成する

var boby = "{ \"id\": \"bob\",\"firstName\": \"Bob\", \"lastName\": \"LeBricoleur\", \"email\": \"[email protected]\", \"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); 
         } 

[OK]を、これは...私に何も与えないようにさえエラーメッセージ:VARここでこの方法は)ので、私は、データを投稿するのXMLHttpRequestが含まれているこのコードを書きました。私はそれが私の全体のコードのためだと思ったので、デバッグを簡略化するためにサーバーに送る変数bobyを作成しましたが、エラーの原因はまだ分かりません。

私のコードから問題がありますか? APIを使用する私の方法からですか?アルフレツコの共有から?あなたが私に多くの感謝を助けることができれば!

PS:それは助けることができる場合、私は同じURLを使用して動作します(代わりに1を作成する)人をリストアップするためのコードを書いた


[EDIT]

は、私は別の道へのを見つけた

私の要求を作成し、それが動作しますが、私はなぜ知らない...ここにコードです:

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リクエストを再作成し、そのAPIが機能することを確認してください。そうすれば、その部分が確実に動作することがわかります。 – Lista

+0

使用しているアルフレコのバージョンは? –

+0

@Listaこの部分は働いています。私はPostmanのおかげでそれをやってきました。良い結果が得られました。 – 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

リストの人にとって、リクエストメソッドは取得して投稿しません。 –

+0

申し訳ありませんが、私が上記のようにHTTPリクエストのタイプを意味すると言ったときに、私はGETを使用しました – JackA

関連する問題