简体   繁体   English

jQuery Ajax不响应Node JS

[英]JQuery Ajax does not response to Node JS

I have the following form (in the client side): 我有以下表格(在客户端):

<html>
<body>
<script>
    $(document).ready(function() {
        $.ajax({
            url: "Search.html",
            type: "POST",
            dataType : "json",

            success: function (result) {
                console.log(result);
                if(result.status == 200 && result.validationFailed ){
                    alert(result.message);
                }
            },

            error: function(result){
                console.log(result);
                if(error.responseText == 'showAlert'){
                    alert( "Sorry, there was a problem!" );
                }
            }
            })
            });
    }   
    </script>
    <form action="/process_post" method="POST">
        <select name="SearchTypes">
            <option value="Author" selected>Author</option>
            <option value="Mention">Mention</option>
            <option value="Tag">Tag</option>
        </select>
        <input type="text" name="term">
        <br><br>
        <input type="submit" value="Submit">
    </form>

</body>
</html>

In the server side, I am using Node JS to validate data against the database and reply back to the client. 在服务器端,我正在使用Node JS针对数据库验证数据并回复给客户端。 I have one case where there is no data in the database and I want to tell the user using Ajax data are not available: 我有一种情况,数据库中没有数据,我想告诉使用Ajax的用户不可用:

var query = connection.query(queryString, [term,term], function(err, rows) {
        var res1 = {};
        console.log(rows);
        var tweet = JSON.parse(JSON.stringify(rows));
        if (tweet.length == 0){
            res1.status = 200;
            res1.validationFailed = true;
            res1.message = 'Empty data';
            res.send(JSON.stringify(res1));
        }else{
            for(var i in tweet){
            res.write("Author: ");
......

When I run the code, I do not get an alert. 运行代码时,我没有收到警报。 I actually get the following: 我实际上得到以下信息:

{"status":500,"validationFailed":true,"message":"Empty data"}

Can you please help me with this 你能帮我吗

您忘记在$ .ajax中设置数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM