开发者

Changing the get code to use the post protocol

I want to change the below code to use a POST request. How can I achieve that?

function getRecord(){
   xhr.open("GET", "items.php?id=" + Number(new Date), true);
   xhr.onreadystatechange = ge开发者_StackOverflow社区tData;
   xhr.send(null);
}


The code below should work for you. Not that, apart from changing "GET" to "POST", you will also need to do a few other things - send the header, and send the parameters separately (last line). Currently, you are not sending any parameters (your last line); they are encoded the same way as your GET parameters, just without the "?" at the beginning.

function getRecord() {
    var params = "id=" + Number(new Date);
    xhr.open("POST", "items.php", true);

    //Send the proper header information along with the request
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.setRequestHeader("Content-length", params.length);
    xhr.setRequestHeader("Connection", "close");

    xhr.onreadystatechange = getData;
    xhr.send(params);
}


You would need to create a FormData object like it says on https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects#Creating_a_FormData_object_from_scratch

But it would only work on Gecko 2 Browsers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜