我对Ajax使用POST方法,并且我的服务器路由位于node
和express.js
上。我的路线回复了数据,但我的请求仍处于待处理状态,并且没有返回任何响应。
客户端请求调用
$('#select-category').change(function(e){
var category = prompt("Enter category name : ", "hello");
var inputData = {
category_name : category,
number_of_contents : 0
};
$.ajax({
method: "POST",
url: "/category/add",
data : inputData,
success: function(response){
console.log('res = ', response);
if(response.status == true){
$.ajax({
method: "GET",
url: "/category/all",
dataType: 'json',
success: function (data, textStatus, jqXHR) {
var returned_data = data;
console.log("returned_data ="+returned_data);
callback(returned_data);
},
error : function(error){
}
});
}else{
}
},
error : function(error){
}
});
})
“我的服务器端路由代码”为
app.get('/category/all', async function(req, res){
console.log('hello world');
var data = await quizController.getCategory(req);
res.writeHead(200, {'content-type': 'text/json' });
res.write( JSON.stringify({ data : data}) );
res.end('\n');
})
您知道如何解决此问题吗?
答案 0 :(得分:1)
尝试type:"POST"
:
$('#select-category').change(function(e){
var category = prompt("Enter category name : ", "hello");
var inputData = {
category_name : category,
number_of_contents : 0
};
$.ajax({
type: "POST", // Its type
url: "/category/add",
data : inputData,
success: function(response){
console.log('res = ', response);
if(response.status == true){
$.ajax({
type: "GET",
url: "/category/all",
dataType: 'json',
success: function (data, textStatus, jqXHR) {
var returned_data = data;
console.log("returned_data ="+returned_data);
callback(returned_data);
},
error : function(error){
}
});
}else{
}
},
error : function(error){
}
});
});
服务器路由:
app.post('/category/add',function(req, res){
.
.
.
.
res.writeHead(200, {'content-type': 'text/json' });
res.write( JSON.stringify({ data : data}) );
res.end('\n');
});