Node.js中待处理的呼叫/请求

时间:2019-01-08 09:32:29

标签: jquery node.js ajax express

我对Ajax使用POST方法,并且我的服务器路由位于nodeexpress.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');
    })

enter image description here

您知道如何解决此问题吗?

1 个答案:

答案 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');
});