[英]AJAX call and getting return data in Node
I'm wanting to make an ajax call from the client to the backend. 我想从客户端到后端进行ajax调用。 I get a successful call from the success function, however, I can't understand how I get data from the server to return from the client. 我从成功函数中获得了成功的调用,但是,我不明白如何从服务器获取数据以从客户端返回。
currently my error trying to use res.send is: 目前我尝试使用res.send的错误是:
Error: Can't set headers after they are sent.
function getProfessorResults() {
var textData = $('#inputsm').val();
var data = {user:"gopal@gmail.com"};
$.ajax({
url: 'http://localhost:3000',
data: { theme: "somevalue", snippet: { name: "somename", content: "somevalue" } },
method: 'POST',
async: false,
cache: false,
timeout: 5000,
contentType: "application/json",
success: function(data) {
console.log("success");
},
complete: function(data) {
console.log("completed");
},
error: function(jqXHR, textStatus, errorThrown) {
alert('Error connecting to the Node.js server... ' + textStatus + " " + errorThrown);
}
});
}
exports.home = function(req, res) {
function passList(profArray, callback) {
setTimeout(function () {
callback(profArray);
}, 1000);
}
function getProfs(teacher_name, successCallback) {
google.resultsPerPage = 10
var nextCounter = 0
google(teacher_name, function (err, res){
if (err) console.error(err)
var teacher_results = []; //Hold all the teachers returned from the function
for (var i = 0; i < res.links.length; ++i) {
var link = res.links[i];
if (!link.title.includes('Add') || !link.title.includes('RATINGS') || !link.title.includes("Hint")) {
teacher_results.push(link.title);
}//End if for comparisons ||
} //End For
successCallback(teacher_results);
}); //End google function
teacher_results = ['tester1', 'tester2'];
successCallback(teacher_results);
} //End searchForProfessor
getProfs(teacher_name, function(data) {
prof_list = data;
console.log(prof_list);
return true;
});
if (req.method == 'POST'){
console.log("true");
// dataReceived = JSON.parse(req);
// console.log(dataReceived);
var obj = {
tid: 'ryan'
};
res.send(JSON.stringify(obj));
}
res.render('home', {
profs: prof_list,
dataStuff : dataReceived
});
};
In the backend, you should have some route where your AJAX call lands. 在后端,您应该有一条到达AJAX呼叫的路由。 In there, you can invoke send
on your response. 在这里,您可以调用send
响应。
In node.js/express, this would look something like 在node.js / express中,这看起来像
app.get('/ajaxURL', function (req, res) {
res.send('I want this string to return to the client');
});
To access the data from the frontend, access it in your AJAX callback: 要从前端访问数据,请在AJAX回调中访问它:
$.ajax({url: '/ajaxURL'}).done(function (data) {
console.log(data);
});
I am not getting the context properly but you can figure out by this example . 我没有正确地了解上下文,但是您可以通过此示例弄清楚。
Sending data from server response.send("Your data");
从服务器response.send("Your data");
发送数据response.send("Your data");
Access this data in your client in success method of AJAX: success:function(data){console.log(data)};
通过AJAX的成功方法在客户端中访问此数据: success:function(data){console.log(data)};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.