最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

json - Sending Http POST Request through Rest API in Javascript - Stack Overflow

programmeradmin0浏览0评论

I am trying to send an Http post request to parse server through Rest API keys. Not sure if I am doing it right as below. The following is my whole script and makes a button which should trigger the post request in a simple HTML page.

<input id="clickMe" type="button" value="clickme" onclick="doFunction();" />
<script>

xmlhttp = new XMLHttpRequest();
var url = "";
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-type", "application/json");
xmlhttp.setRequestHeader("X-Parse-Application-Id", "VnxVYV8ndyp6hE7FlPxBdXdhxTCmxX1111111");
xmlhttp.setRequestHeader("X-Parse-REST-API-Key","6QzJ0FRSPIhXbEziFFPs7JvH1l11111111");
xmlhttp.onreadystatechange = function () { //Call a function when the state changes.
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        alert(xmlhttp.responseText);
    }
}
var parameters = {
    "ephrase": "english",
    "pphrase": "farsi",
     "nvote": 0,
    "yvote": 0
};
// Neither was accepted when I set with parameters="username=myname"+"&password=mypass" as the server may not accept that

function doFunction() {
  xmlhttp.send(parameters);
}

</script>

I am trying to send an Http post request to parse.com server through Rest API keys. Not sure if I am doing it right as below. The following is my whole script and makes a button which should trigger the post request in a simple HTML page.

<input id="clickMe" type="button" value="clickme" onclick="doFunction();" />
<script>

xmlhttp = new XMLHttpRequest();
var url = "https://api.parse.com/1/classes/english";
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-type", "application/json");
xmlhttp.setRequestHeader("X-Parse-Application-Id", "VnxVYV8ndyp6hE7FlPxBdXdhxTCmxX1111111");
xmlhttp.setRequestHeader("X-Parse-REST-API-Key","6QzJ0FRSPIhXbEziFFPs7JvH1l11111111");
xmlhttp.onreadystatechange = function () { //Call a function when the state changes.
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        alert(xmlhttp.responseText);
    }
}
var parameters = {
    "ephrase": "english",
    "pphrase": "farsi",
     "nvote": 0,
    "yvote": 0
};
// Neither was accepted when I set with parameters="username=myname"+"&password=mypass" as the server may not accept that

function doFunction() {
  xmlhttp.send(parameters);
}

</script>
Share Improve this question asked Jun 22, 2015 at 9:42 AmirAmir 1,6833 gold badges26 silver badges42 bronze badges 2
  • 1 what error are you getting? – Obscure Geek Commented Jun 22, 2015 at 9:43
  • I get no error. There is just nothing happening. – Amir Commented Jun 26, 2015 at 2:15
Add a comment  | 

1 Answer 1

Reset to default 13
xmlhttp.send(parameters);
             ^^^^^^^^^^

That needs to be a string, but it is an object, so will be converted to the string: "[object Object]".

You need to convert the data to the proper encoding first.

You've said:

xmlhttp.setRequestHeader("Content-type", "application/json");

so you can use JSON.stringify(parameters) for that.

发布评论

评论列表(0)

  1. 暂无评论