doufei16736
2017-03-17 11:15
浏览 158

$ .post函数不起作用,没有错误[关闭]

I want to retrieve data from a mySQL server by using jQuery, which triggers a PHP script to receive the data. However, the $.post function from jQuery does not work at all and corrupts all the code, and I don't understand why.

Here is my JS code:

$('button#btnSubmit').on('click', function() {
    //Compare button pressed
    var sel1 = $('select#country1').val(); //country iso codes
    var sel2 = $('select#country2').val();

    if(sel1 === "placeholder1" || sel2 === "placeholder2" || sel1 === sel2) {
        alert("Please select at least two different countries")
    }
    else {
        //post this to php file to retrieve data
        try {
            $.post('ajax/retrieve_data.php', { sel1: sel1, sel2: sel2 }, function(data) {
                $('div#test-data').text(data);
        }
        catch(err) {
            alert(err.message);
        }
    }
});

I included the jQuery library in my main HTML page with this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

The $.post message corrupts the entire code, nothing works. When I remove it, the first part of the code works as it should. I have the feeling this is extremly easy to fix but I can't wrap my head around why it does not work. Thank you!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douxing6434 2017-03-17 11:19
    已采纳

    You are not closing parentheses of $.post

    $('button#btnSubmit').on('click', function() {
        //Compare button pressed
        var sel1 = $('select#country1').val(); //country iso codes
        var sel2 = $('select#country2').val();
    
        if(sel1 === "placeholder1" || sel2 === "placeholder2" || sel1 === sel2) {
            alert("Please select at least two different countries")
        }
        else {
            //post this to php file to retrieve data
            try {
                $.post('ajax/retrieve_data.php', { sel1: sel1, sel2: sel2 }, function(data) {
                    $('div#test-data').text(data);
               });
            }
            catch(err) {
                alert(err.message);
            }
        }
    });
    

    try this :)

    打赏 评论
  • dongyingjiu0669 2017-03-17 11:19

    You are missing a } in your code after the callback function.

    $('button#btnSubmit').on('click', function() {
        //Compare button pressed
        var sel1 = $('select#country1').val(); //country iso codes
        var sel2 = $('select#country2').val();
    
        if(sel1 === "placeholder1" || sel2 === "placeholder2" || sel1 === sel2) {
            alert("Please select at least two different countries")
        }
        else {
            //post this to php file to retrieve data
            try {
                $.post('ajax/retrieve_data.php', { sel1: sel1, sel2: sel2 }, function(data) {
                    $('div#test-data').text(data);
                } // <-- was missing
            }
            catch(err) {
                alert(err.message);
            }
        }
    });
    
    打赏 评论

相关推荐 更多相似问题