Answer the question
In order to leave comments, you need to log in
Why can't I send a POST request with ajax to localhost:3000 (server - node.js)?
There is a form whose data is sent to the server using an AJAX request. The script looks like this:
$(document).ready(function() {
$("#btn").click(
function(){
sendAjaxForm('result_form', 'ajax_form', 'http://127.0.0.1:3000/users');
return false;
}
);
function sendAjaxForm(result_form, ajax_form, url) {
$.ajax({
url: url,
type: "POST",
dataType: "jsonp",
data: $("#"+ajax_form).serialize(),
success: function(response) {
result = $.parseJSON(response);
$('#result_form').html('Имя: '+result.firstname+'<br>Фамилия: '+result.lastname+'<br>Пол: '+result.sex+'<br>Телефон: '+result.phone+'<br>E-mail:'+result.email+'<br>Дата рождения:'+result.birthdate);
//console.log("Done");
},
error: function(response) {
$('#result_form').html('Ошибка. Данные не отправлены.');
}
});
}
});
crossDomain: true,
Answer the question
In order to leave comments, you need to log in
CORS needs to be registered on the server
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, PUT, PATCH, POST, DELETE");
res.header("Access-Control-Allow-Headers", "Content-Type");
next();
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question