关于使用 ajax 发布请求的问题
Issue regarding posting a request using ajax
我是 ajax 和 jquery 的新手。我已经经历了几个关于我遇到的问题的话题,但没有解决任何问题。我在这里遇到两个错误。
OPTIONS http://localhost:8082/xxx/xxx/xxxx 404 (Not Found)
Failed to load http://localhost:8082/xxx/xxx/xxxx: Response for preflight does not have HTTP ok status.
这是我的简单代码
$.ajax({
url: url,
crossDomain: true,
type: "POST",
data: JSON.stringify(queryData),
contentType: "application/json"
}).done(function(msg){
$("#thank_you_comment").text("Thank you");
$("button").text("GOT IT");
queryData.id = msg.id
$.ajax({
url: another_url,
crossDomain: true,
type: "POST",
data: JSON.stringify(queryData),
contentType: "application/json"
}).done(function(message){
event.preventDefault();
console.log(message);
return;
});
});
我正在进行的第二个 ajax 调用是 vertx 上的本地调用。我非常确定另一台本地服务器已启动并且 运行.
但我不确定为什么会出现未找到错误。
非常感谢任何帮助。
感谢您的上述评论,帮助我理解了问题。为了解决这个错误,我没有直接从客户端发布请求,而是将请求发送到服务器端,然后从服务器端调用请求,这完全消除了 CORS 问题并提供了对数据的更多控制。
从客户端,我这样做了
$.ajax({
url: /post/request-to,
type: "POST",
data: JSON.stringify(queryData),
contentType: "application/json"
}).done(function(message){
event.preventDefault();
console.log(message);
return;
});
然后在服务器脚本中
app.post('/post/request-to', function(req, res){
try{
request.post({
headers: {'content-type' : 'application/json'},
url: "http://some/url",
body: JSON.stringify(req.body)
}, function(error, response, body){
if(error){
console.log("Error: "+error)
}else{
res.send(body)
}
});
console.log(req.body);
}catch(err){
console.log(err)
}
});
我是 ajax 和 jquery 的新手。我已经经历了几个关于我遇到的问题的话题,但没有解决任何问题。我在这里遇到两个错误。
OPTIONS http://localhost:8082/xxx/xxx/xxxx 404 (Not Found)
Failed to load http://localhost:8082/xxx/xxx/xxxx: Response for preflight does not have HTTP ok status.
这是我的简单代码
$.ajax({
url: url,
crossDomain: true,
type: "POST",
data: JSON.stringify(queryData),
contentType: "application/json"
}).done(function(msg){
$("#thank_you_comment").text("Thank you");
$("button").text("GOT IT");
queryData.id = msg.id
$.ajax({
url: another_url,
crossDomain: true,
type: "POST",
data: JSON.stringify(queryData),
contentType: "application/json"
}).done(function(message){
event.preventDefault();
console.log(message);
return;
});
});
我正在进行的第二个 ajax 调用是 vertx 上的本地调用。我非常确定另一台本地服务器已启动并且 运行.
但我不确定为什么会出现未找到错误。
非常感谢任何帮助。
感谢您的上述评论,帮助我理解了问题。为了解决这个错误,我没有直接从客户端发布请求,而是将请求发送到服务器端,然后从服务器端调用请求,这完全消除了 CORS 问题并提供了对数据的更多控制。
从客户端,我这样做了
$.ajax({
url: /post/request-to,
type: "POST",
data: JSON.stringify(queryData),
contentType: "application/json"
}).done(function(message){
event.preventDefault();
console.log(message);
return;
});
然后在服务器脚本中
app.post('/post/request-to', function(req, res){
try{
request.post({
headers: {'content-type' : 'application/json'},
url: "http://some/url",
body: JSON.stringify(req.body)
}, function(error, response, body){
if(error){
console.log("Error: "+error)
}else{
res.send(body)
}
});
console.log(req.body);
}catch(err){
console.log(err)
}
});