weixin_33686714 2015-02-22 14:57 采纳率: 0%
浏览 29

无法拨打AJAX电话

I am trying to insert data into the database using AJAX. An ajax call goes to a servlet, that is meant to insert data into the database. But I think I have made a mistake somewhere in initializing ajax object. When I click on the submit button,data doesn't get submitted to the database.

HTML:

<form class='form-inline'>
                    <div class='form-group'>
                        <label for='nameField'>Name</label>
                        <input type='text' class='form-control' id='nameField'name='nameField' placeholder='David'>
                    </div>
                    <div class='form-group'> 
                        <label for='goalField'>Goals Scored</label>
                                                    <input type='text' class='form-control' id='goalField' name="goalField" placeholder='0'>
                    </div>
                    <div class='form-group'>
                        <label for='passField'>Passes Made</label>
                                                    <input type='text' class='form-control' id='passField' name="passField" placeholder='0'>
                    </div>
                                    <button type='submit' class='btn btn-primary' id='submitdata'>Submit to database</button>
</form>

JQuery :

<script>

        $(document).ready(function() {
            $('#submitdata').click(function(event) {
                event.preventDefault();
                alert('clicked');
                if(window.XMLHttpRequest) {                        
                    $xhr = new XMLHttpRequest();
                    $xhr.onreadystatechange = function() {
                        if($xhr.readyState === 4 && $xhr.status === 200) {
                            $xhr.open("GET","insert","true");
                            $xhr.send();
                        }
                    }
                } else {alert('else statement');}
            });
        });     

</script>

Where did I make a mistake?

  • 写回答

1条回答 默认 最新

  • weixin_33694172 2015-02-22 15:04
    关注

    You should call open() and send() outside ready state change listener not from within the callback :)

        $('#submitdata').click(function(event) {
                event.preventDefault();
                alert('clicked'); // DOESN'T GO BEYOND THIS
                if(window.XMLHttpRequest) {                        
                    $xhr = new XMLHttpRequest();
                    // bind the readystage change listener first
                    $xhr.onreadystatechange = function() {
                        if($xhr.readyState === 4 && $xhr.status === 200) {
                            alert('response received');
                        }
                    }
                    // call open passing request type, url, async
                    $xhr.open("GET","/context-root/insert.do",true);
                    $xhr.send();
    
                } else {
                   alert('else statement');
                } // DOESN'T EVEN REACH HERE
          });
    

    Also you can use load event to handle response

    Using jQuery, you can do something like this

     $('#submitdata').click(function(event) {
            event.preventDefault();
            $.ajax({
              'url' : '/ctxRoot/insert',
              'type': 'GET' // default is GET
            })
            .done(function(data){
                console.log('Ajax response - '+data);
            });
      });
    

    For more details, check the official documentation here

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题