doujue6196 2018-05-13 03:04
浏览 295

javascript中没有错误但它不起作用

I use PHP to echo back javascript and html. I am trying to send two values to another PHP file using AJAX with the code echoed back from the original echo. An example of this javascript is:

<script>
                $(document).ready(function () {
                    $('#join_testtest').on('cick', function() {
                        var clubName = $('#join_name_testtest').val();
                        var userName = $('#userName_join').val();

                        $.ajax({
                                  url: 'joinClub.php',
                                  method: 'POST',
                                  data: {
                                      clubName: clubName,
                                      userName: userName
                                  },
                                  success: function(response){
                                        $('#response_join').html(response); 
                                    }, 
                                    dataType: 'text'
                               });

                    });
                });

            </script>

Here is the html code that corresponds to the above example:

<input id='join_testtest' type='button' value='join testtest'>
<input type='text' id='join_name_testtest' style='display:none;' value='testtest'>
<input id='userName_join' style='display:none;' value ='1'>

There are no errors in the console. The PHP will either exit with keyword success or fail but nothing is printed in #response_join. Any help is apreciated.

  • 写回答

2条回答 默认 最新

  • douyongdao4046 2018-05-13 03:09
    关注

    I noticed your click before your arbitrary function was spelled wrong. I changed Ajax so after it is finished it should log an error. If you’re not getting the error logged, then it’s not getting to the Ajax methods.

    I also added FormData object as well.

    <script>
        $('document').ready(function () {
            $('#join_testtest').on('click', function() {
                var clubName = $('#join_name_testtest').val();
                var userName = $('#userName_join').val();
    
                var formData = new FormData();
                formData.append('clubName', clubName);
                formData.append('userName', userName);
    
                $.ajax({
                    url: 'joinClub.php',
                    method: 'POST',
                    data: formData,
                    dataType: 'text'
    
                }).done(response => {
    
                    $('#response_join').html(response); 
    
                }).error(error => {
                    console.log(error);
    
                });
    
            });
        });
    
    </script>
    

    If you can post some more of the code, I think we’re missing something between the DOM elements and jQuery.

    Are you using an old version of JQuery?

    Where is this script located? At the top or bottom of the page? And is it above or below the jQuery inclusion?

    You can also download and test with Postman

    You can also change $('#join_testtest').on('click', function() {

    with

    $('body').on('click', '#join_testtest', function(event){
    event.preventDefault();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog