Ajax没有调用成功功能 [英] Ajax not calling success function

查看:106
本文介绍了Ajax没有调用成功功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ajax来验证from,而无需重新加载页面.

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

脚本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"; ?>

我的问题是ajax不调用成功函数,我总是出现"ERROR"文本...

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

目前我还没有requeteSpecimen.php文件的代码,我只是想调用成功函数.不知道它是否可以解决问题,但我使用的是cakePHP 3.0.

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.

非常感谢.

推荐答案

您不会在Cakephp中调用类似的操作.

You do not call actions like that in Cakephp.

首先在您的一个Controller中设置一个Action,然后从您的$ .ajax函数中调用url.

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>';
            }
        });
}

您稍后可以为request_specimen添加一条路线,并使它更漂亮.

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

在您的Controller中,应该有一个名为:public function request_specimen() {}的方法,Wich将像这样处理ajax请求:if($this->request->is(['ajax']) { /* Handle request */}

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 */}

发送到该函数的数据将在$ this-> request-> data;

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

这篇关于Ajax没有调用成功功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆