gpt4 book ai didi

javascript - 将参数传递给 XMLHttpRequest 对象

转载 作者:可可西里 更新时间:2023-11-01 02:54:44 25 4
gpt4 key购买 nike

如何将参数传递给 XMLHttpRequest 对象?

function setGUID(aGUID) {

var xhReq = new XMLHttpRequest();

xhReq.open("POST", "ClientService.svc/REST/SetAGUID" , false);
xhReq.send(null);
var serverResponse = JSON.parse(xhReq.responseText);
alert(serverResponse);
return serverResponse;
}

我需要使用 javascript 而不是 jquery,在 jquery 中我可以使用这段代码,但似乎无法用直接的 javascript 方式来解决它..

function setGUID(aGUID) {

var applicationData = null;

$.ajax({
type: "POST",
url: "ClientService.svc/REST/SetAGUID",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ aGUID: aGUID }),
dataType: "json",
async: false,
success: function (msg) {

applicationData = msg;

},
error: function (xhr, status, error) { ); }
});

return applicationData;

}

最佳答案

网上有很多关于“xmlhttprequest post”的教程。我只是复制其中一个:

看看:

http://www.openjs.com/articles/ajax_xmlhttp_using_post.php

https://www.google.com/search?q=xmlhttprequest+post

var http = new XMLHttpRequest();
var url = "url";
var params = JSON.stringify({ appoverGUID: approverGUID });
http.open("POST", url, true);

http.setRequestHeader("Content-type", "application/json; charset=utf-8");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);

关于javascript - 将参数传递给 XMLHttpRequest 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12676698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com