2016-05-05 1 views
0

JSONでRest Webサービスリクエストを送信しようとしています。しかし、それはを要求します(http 400)エラーです。アプリケーションにログインする必要はありません。私はさまざまなフォーマットを試しましたが、何も助けませんでした。主な問題は、JSON変数が動的であることです。つまり、ユーザーが入力した値です。JavaScriptで変数を持つJSON

var jsonObject = []; 
var first_name = document.getElementById("firstName").value; 
var last_name = document.getElementById("lastName").value; 
var u_password = document.getElementById("password").value; 
var u_password_hint = document.getElementById("passwordHint").value; 
var email = document.getElementById("email").value; 
var u_phone = document.getElementById("phone").value; 
var u_organization = document.getElementById("organization").value; 

var user = new User(first_name,last_name,email,u_password,u_password_hint,u_organization); 
jsonObject.push(user); 

var client = new XMLHttpRequest(); 
client.open("POST","https://instance.service-now.com/api/now/v1/table/user_registration_request"); 
client.setRequestHeader('Accept','application/json'); 
client.setRequestHeader('Content-Type','application/json'); 
client.send(body); 
window.location = 'activate account.do'; 

function User(first_name, last_name, email, u_password_hint, u_password, u_organization) { 
    this.first_name = first_name; 
    this.last_name = last_name; 
    this.email = email; 
    this.u_password = u_password; 
    this.u_password_hint = u_password_hint; 
    this.u_organization = u_organization; 
} 

答えて

0

は、トラブルの多くの後、私が見つけましたソリューション:オリジナルのJSON形式は、わずかな変更で動作します。

var xhr = new XMLHttpRequest(); 
xhr.withCredentials = true; 
xhr.addEventListener("readystatechange", function() { 
if (this.readyState === 4) { 
console.log(this.responseText); 
} 
}); 
xhr.open("POST","https://instance.service-now.com/api/now/table/ecc_queue"); 

var data='{"agent":"AttachmentCreator","topic":"AttachmentCreator","name":"'+fileName+'","source":"u_itinerary_attchements:"'+sysid+'","payload":"'+encodedFile+'"}'; 

xhr.setRequestHeader("authorization", "Basic anBhdmFuOmpwYXZhbg=="); 
xhr.setRequestHeader("Content-Type", "application/json");  
xhr.setRequestHeader("Accept", "application/json"); 
xhr.setRequestHeader("cache-control", "no-cache"); 

xhr.send(data); 
      }; 

注意:一重引用符と二重引用符に注意してください。私はこれらを混乱させ、それを修正するために多くの時間がかかりました。

1

私はServiceNowのREST API explorerであなたのAPIをテストすることをお勧めします。

あなたのコードはテストされていませんが、ユーザー関数は "user"オブジェクトを返さなければなりません。あなたは "新しい" VARのユーザーを使用する必要はありません

client.send(JSON.stringify(user)); 

にJSONとして渡すことができ

function User(first_name, last_name, email, u_password_hint, u_password, 
    u_organization) { 
    return { 
    'first_name': first_name, 
    'last_name': last_name, 
    'email': email, 
    'u_password': u_password, 
    'u_password_hint': u_password_hint, 
    'u_organization': u_organization, 
    }; 
} 

= 新しいユーザー(