Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.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_Javascript_Jquery_Jquery Ui_Cakephp - Fatal编程技术网

在ajax成功中包含cakePHP元素

在ajax成功中包含cakePHP元素,php,javascript,jquery,jquery-ui,cakephp,Php,Javascript,Jquery,Jquery Ui,Cakephp,我有一个javascript代码,它向URL发出Ajax请求,并在成功时(在成功回调中)调用cakePHP元素,如下所示: $.ajax({ type: 'POST', url: "the_url_where_the_request_should_go", success: function(data) { "<?php echo $this->element('the_path_to_my_element'); ?>"; } }

我有一个javascript代码,它向URL发出Ajax请求,并在成功时(在成功回调中)调用cakePHP元素,如下所示:

$.ajax({
    type: 'POST',
    url: "the_url_where_the_request_should_go",
    success: function(data) {
        "<?php echo $this->element('the_path_to_my_element'); ?>";
    }
});
$.ajax({
键入:“POST”,
url:“请求应该去的地方”,
成功:功能(数据){
"";
}
});
如您所见,因为我使用的是javascript代码,所以我必须在PHP标记周围加上双引号(“”),这样才能工作。我遇到的问题是,在success函数中,当我调用我的元素时,会同时发送双引号。也就是说,如果我的元素文件中的代码是:

<div>element_code_here</div>
element\u code\u此处
我得到:

"<div>element_code_here</div>" (notice the double quotes gets included also)
“element\u code\u此处”(注意,双引号也包含在内)
有人知道它为什么会这样做吗?我该如何修复它

顺便说一句,如果我只是删除PHP标记周围的双引号,它就不会工作(它会给我一条javascript无效标识符错误消息)


提前感谢

成功回调仍然是一个javascript函数,因此您需要将使用PHP进行回显的元素视为字符串。我不能100%确定您要完成什么,我可能建议返回元素(html)从ajax端点开始,而不是将其直接插入脚本中,但使用您的示例,您应该执行以下操作:

$.ajax({
    type: 'POST',
    url: "the_url_where_the_request_should_go",
    success: function(data) {
        var html_string = "<?php echo $this->element('the_path_to_my_element'); ?>";
        //do something with the html string.. like insert it into a waiting div
        $('div.container').html(html_string);
    }
});
//Function to call with $.ajax

function giveElement($elementName){

    //logic to do with POST


    //element to return
    $this->render('/elements/'.$elementName);


}
//NOT TESTED. This might need something else too.
$.ajax({
键入:“POST”,
url:“请求应该去的地方”,
成功:功能(数据){
var html_string=“”;
//对html字符串执行一些操作..比如将其插入等待的div中
$('div.container').html(html_字符串);
}
});

胡乱猜测。尝试使用以下内容从服务器返回元素:

$.ajax({
    type: 'POST',
    url: "the_url_where_the_request_should_go",
    success: function(data) {
        var html_string = "<?php echo $this->element('the_path_to_my_element'); ?>";
        //do something with the html string.. like insert it into a waiting div
        $('div.container').html(html_string);
    }
});
//Function to call with $.ajax

function giveElement($elementName){

    //logic to do with POST


    //element to return
    $this->render('/elements/'.$elementName);


}
//NOT TESTED. This might need something else too.

还在为这个挣扎吗?