admin管理员组

文章数量:1429791

new to this side of things and can't seem to find the proper syntax. In the POST request payload, I need to include "grant_type=client_credentials" according to the service I'm trying to access. However, I can't find the JavaScript syntax to include the username and password in the request payload. Here is what I have so far (I have removed my key and secret for obvious reasons):

    var request = new XMLHttpRequest();
    var path=".1/auth/accesstoken";
    request.onreadystatechange=state_change;

    var encodedData = window.btoa(Key:Secret"); //encode consumer key and secret key

    document.write(encodedData);

    request.open("POST", path, true);
    request.setRequestHeader("Authorization", encodedData);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    request.send(null);

    function state_change() {
        if (request.readyState == "4") //Request finished and response is ready
        {
            if (request.status == "200") {


   alert("Success");
        }
        else {
            alert("Problem retrieving data");
            console.log(this.responseXML);
        }
    }
}

Thanks in advance, been stuck on this for the last few hours.

new to this side of things and can't seem to find the proper syntax. In the POST request payload, I need to include "grant_type=client_credentials" according to the service I'm trying to access. However, I can't find the JavaScript syntax to include the username and password in the request payload. Here is what I have so far (I have removed my key and secret for obvious reasons):

    var request = new XMLHttpRequest();
    var path="https://ops.epo/3.1/auth/accesstoken";
    request.onreadystatechange=state_change;

    var encodedData = window.btoa(Key:Secret"); //encode consumer key and secret key

    document.write(encodedData);

    request.open("POST", path, true);
    request.setRequestHeader("Authorization", encodedData);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    request.send(null);

    function state_change() {
        if (request.readyState == "4") //Request finished and response is ready
        {
            if (request.status == "200") {


   alert("Success");
        }
        else {
            alert("Problem retrieving data");
            console.log(this.responseXML);
        }
    }
}

Thanks in advance, been stuck on this for the last few hours.

Share Improve this question edited Apr 13, 2016 at 0:39 TechJunkie asked Apr 12, 2016 at 23:04 TechJunkieTechJunkie 1331 gold badge1 silver badge9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

Just needed to remove null from the send payload and replace with 'grant_type=client_credentials'.

var request = new XMLHttpRequest();
var path="https://ops.epo/3.1/auth/accesstoken";
request.onreadystatechange=state_change;

var encodedData = window.btoa("Key:secret"); //encode consumer key and secret key

request.open("POST", path, true);
request.setRequestHeader("Authorization", encodedData);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

request.send('grant_type=client_credentials');

function state_change() {
    if (request.readyState == "4") //Request finished and response is ready
    {
        if (request.status == "200") {
            alert("Success");
            document.write(request.responseText);;

        }
        else {
            alert("Problem retrieving data");
            console.log(this.responseXML);
        }
    }
}

本文标签: javascriptXMLHTTPRequest Returning Bad Request (400)quotRequire Param granttypequotStack Overflow