还是报错,怀疑是c++代码的问题:
Access to XMLHttpRequest at 'http://192.168.2.22:88/' from origin 'http://127.0.0.1:8080' has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.
c++做了跨域:
void HttpResponse::addHeader(Buffer& buffer)
{
buffer.append("Content-Type: text/html; charset=UTF-8\r\n");
buffer.append("Connection: ");
if(isKeepAlive)
{
buffer.append("keep-alive\r\n");
}
else
{
buffer.append("close\r\n");
}
buffer.append("Content-type: application/json\r\n");
buffer.append("Access-Control-Allow-Origin:*\r\n");
前端js:
result.addEventListener("mouseover", function(){
const xhr = new XMLHttpRequest();
// console.log("test");
//xhr.open('GET', 'http://121.41.165.74:8888/getjson');
xhr.open('POST', 'http://121.41.165.74:8888');
// xhr.send('json_data=');
// json object1 = R"({"ProjName":"*********************hn","FileName":"eeee .catpart", "ProjNumber":30, "JpgFile":"kk.jpg","Comments": "cdr for employee ","ProjType": "graphic design","ProjState": 1, "version": 1,"CreateTime": " 20230728","UpdateTime": "20230608","pid": 6})"
var jsonObject = {
"ProjName": "*********************hn",
"FileName": "eeee.catpart",
"ProjNumber": 30,
"JpgFile": "kk.jpg",
"Comments": "cdr for employee",
"ProjType": "graphic design",
"ProjState": 1,
"version": 1,
"CreateTime": "20230728",
"UpdateTime": "20230608",
"pid": 6
};
// 将 JSON 对象转换为字符串
var jsonString = JSON.stringify(jsonObject);
// 创建 XMLHttpRequest 对象
//var xhr = new XMLHttpRequest();
//var url = "your_backend_url_here"; // 替换成你的后端接口地址
// 设置 POST 请求
// xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
// 发送请求
xhr.send(jsonString);
xhr.onreadystatechange=function(){
if(xhr.readyState === 4){
if(xhr.status >= 200 && xhr.status <300){
result.innerHTML = xhr.response;
} }}
});