Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
重定向不工作。$。ajax+Cakephp_Php_Jquery_Ajax_Cakephp - Fatal编程技术网

重定向不工作。$。ajax+Cakephp

重定向不工作。$。ajax+Cakephp,php,jquery,ajax,cakephp,Php,Jquery,Ajax,Cakephp,你好 我有一个无法解决的问题,我正在使用Cakephp和$.ajax。来自$.ajax的数据可以传递到我的数据库中。但问题是成功后我无法重定向到其他页面 我尝试了if$save{echo something}并且正在工作,只是$this->redirect部分 Cakephp代码如下所示: public function testingadd() { $this->layout=null; $name = $_GET['name']; $email = $

你好

我有一个无法解决的问题,我正在使用Cakephp和$.ajax。来自$.ajax的数据可以传递到我的数据库中。但问题是成功后我无法重定向到其他页面

我尝试了if$save{echo something}并且正在工作,只是$this->redirect部分

Cakephp代码如下所示:

public function testingadd() {
        $this->layout=null;

    $name = $_GET['name'];
    $email = $_GET['email'];
    $phone = $_GET['phone'];

    $this->Newlead->create();


    $this->Newlead->set("name",$name);
    $this->Newlead->set("email",$email);
    $this->Newlead->set("phone",$phone);


    $save = $this->Newlead->save();

    if($save)
    {
        $this->redirect('/Newlead/thankyou');
    }

 }
Ajax代码如下所示:

$("#btn-submit").click(function ()
    {

    var obj = new Object();
    obj.n = $("#inputName").val();
    obj.e = $("#inputEmail").val();
    obj.c = $("#inputMobile").val();

        $.ajax({
            type: 'POST',
            url: '/Newleads/testingadd.json',
            data: {
                'name' : obj.n,
                'email' :  obj.e,
                'phone' :  obj.c
            },
            dataType: "jsonp",
            timeout:1000,
            jsonp:'jsonp'
        });
});

是否需要向Cakephp中的任何文件(如routes.php、PagesController.php或其他文件)添加更多代码?我还是个新手。请提供帮助。

根据您的服务器响应,通过JavaScript在客户端上重定向:

服务器控制器上的代码

客户端上的代码


不知道问题出在哪里

但我已经解决了

$.ajax({
[...]
dataType:'jsonp', //have to be Jsonp because I'm using different domain.
error:function(){
  redirect(url);
}

这样做有点尴尬。但是,我对找不到的错误感到失望。

您需要将响应从php发送回ajax,并在ajax中添加一个成功块,然后重新发送ajax@RakeshSharma从php到ajax的响应?对于我的'success:functiondata,status,xhr{//response=data setTimeoutredirect,100;}''函数重定向{//alert'thankyou.我们已收到您的详细信息。'window.location='url/thankyou.html';}'也不起作用,错误是未捕获的SyntaxError:Unexpected token
$.ajax({
    [...],
    dataType: 'json',
    success: function(data) {   

        if(data.saveSuccess === 'true')
        {
            // the redirect
            window.location = '/Newlead/thankyou';
        }

    },
    error: function()
    {
        alert('an error occured');
    }            
});
$.ajax({
[...]
dataType:'jsonp', //have to be Jsonp because I'm using different domain.
error:function(){
  redirect(url);
}