Ajax not calling success function

て烟熏妆下的殇ゞ 提交于 2019-12-13 09:48:23

问题


I'm using ajax to validate a from without reloadind the page.

Script Ajax

    function updateResult(tab){
            $.ajax({
                url:"requeteSpecimen.php",
                data:{datas:tab},
                dataType: 'text',
                async:false,
                success: function(data){
                    document.getElementById('resultat').innerHTML = '<p>'+data+'</p>';
                },
                error: function(data){
                    document.getElementById('resultat').innerHTML = '<p>ERROR</p>';
                }
            });
    }

    $("#filtre").submit(function(){
        <?php $tab=$this->request->data; ?>
        updateResult(<?php json_encode($tab);?>);
    });

</script>

requeteSpecimen.php

<?php echo "Success"; ?>

My problem is that ajax do not call the sucess function, I always have the "ERROR" text appearing ...

For the moment I don't have yet the code of my requeteSpecimen.php file and I just would like the success function to be called. Don't know if it can help but I'm using cakePHP 3.0.

Many thanks in advance.


回答1:


You do not call actions like that in Cakephp.

First set up an Action in one of your Controllers call the url from your $.ajax function like this.

function updateResult(tab){
        $.ajax({
            url:"/controller/request_specimen",
            data:{datas:tab},
            dataType: 'text',
            async:false,
            success: function(data){
                document.getElementById('resultat').innerHTML = '<p>'+data+'</p>';
            },
            error: function(data){
                document.getElementById('resultat').innerHTML = '<p>ERROR</p>';
            }
        });
}

You could later on add a route for request_specimen and make it prettier.

In your Controller there should be a method called: public function request_specimen() {} Wich will handle the ajax request like so: if($this->request->is(['ajax']) { /* Handle request */}

The data sent to the function will be in $this->request->data;



来源:https://stackoverflow.com/questions/30002886/ajax-not-calling-success-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!