在不使用重定向发送数据的情况下,可以使用以下方法之一来解决问题:
var xhr = new XMLHttpRequest();
xhr.open("POST", "url", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
console.log(xhr.responseText);
}
};
xhr.send(JSON.stringify({data: "your data"}));
这里使用了XMLHttpRequest对象发送POST请求,并使用JSON.stringify方法将数据转换为JSON格式进行发送。
fetch("url", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({data: "your data"})
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
这里使用了fetch API发送POST请求,并使用JSON.stringify方法将数据转换为JSON格式进行发送。
$.ajax({
url: "url",
type: "POST",
contentType: "application/json",
data: JSON.stringify({data: "your data"}),
success: function(response) {
console.log(response);
},
error: function(error) {
console.error(error);
}
});
这里使用了jQuery的ajax方法发送POST请求,并使用JSON.stringify方法将数据转换为JSON格式进行发送。
请注意,以上示例中的"url"应替换为您要发送数据的目标URL。另外,还需要根据您的需求设置合适的请求头和请求体。