我试图向一个节点发送一个ajax post请求。通过nginx的js微服务。要求如下:
$.post('http://localhost:80/api/signup', formData, function( data, status) {
console.log(JSON.stringify(data));
console.log(JSON.stringify(status));
}).done(function(data, status) {
alert( "second success" );
console.log(JSON.stringify(data));
console.log(JSON.stringify(status));
})
.fail(function(data, status) {
console.log('error');
console.log(JSON.stringify(data));
console.log(JSON.stringify(status));
})
.always(function(data, status) {
console.log(JSON.stringify(data));
console.log(JSON.stringify(status));
console.log('fiished');
});
“数据”始终是:
{"readyState":0,"status":0,"statusText":"error"}
“状态”始终是:
error
这不是成功或失败时的预期反应。
此外,“formData”参数在提交后显示为url上的查询。我怎么才能阻止呢?
更新*
我还尝试添加
event.preventDefault();
XMLHttpRequest cannot load http://localhost:3000/api/signup. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
{"readyState":0,"status":0,"statusText":"error"}
我也尝试过:
$.ajax({
type: "POST",
url: "http://localhost:3000/api/signup",
data: JSON.stringify(formData),
success: function(dataString) {
console.log(JSON.stringify(dataString));
}, error: function(error) {
console.log(JSON.stringify(error));
}
});
但还是一样的
No 'Access-Control-Allow-Origin' header is present on the requested resource
同上。
"No 'Access-Control-Allow-Origin' header is present on the requested resource"
然而,我发现我在这里接受的答案更直接、更清楚。