Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.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
Javascript 嵌套Ajax问题。如何调试/解决?_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 嵌套Ajax问题。如何调试/解决?

Javascript 嵌套Ajax问题。如何调试/解决?,javascript,jquery,ajax,Javascript,Jquery,Ajax,有很多关于嵌套Ajax的问题,但我不知道我自己的问题出在哪里 我看到的问题是服务器端脚本在嵌套的Ajax调用中没有接收表单值 在客户端,我得到result2为空 嵌套的Ajax代码用//problem封装 有人知道嵌套Ajax为什么不接收表单值吗 $(document).ready(function(){ $('form').live('submit', function(){ // ... $.ajax({ type: "GET",

有很多关于嵌套Ajax的问题,但我不知道我自己的问题出在哪里

我看到的问题是服务器端脚本在嵌套的Ajax调用中没有接收表单值

在客户端,我得到
result2
为空

嵌套的Ajax代码用
//problem
封装

有人知道嵌套Ajax为什么不接收表单值吗

$(document).ready(function(){
    $('form').live('submit', function(){

    // ...

    $.ajax({
        type: "GET",
        url: "/cgi-bin/ajax_confirm.pl",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        //      async: false,

        data: $(this).serialize(),

        error: function(XMLHttpRequest, textStatus, errorThrown) { 
        $('div#create_result').text("responseText: " + XMLHttpRequest.responseText +
                        ", textStatus: " + textStatus +
                        ", errorThrown: " + errorThrown);
        $('div#create_result').addClass("error");
        alert("Error occured in ajax.js confirm code.");

        },

        success: function(result){
        if (result.error) {
            $('div#create_result').text("result.error: " + result.error);
            $('div#create_result').addClass("error");
        } else { // server side script says everything is okay

            var users  = $.parseJSON(result.users);
            var owners = $.parseJSON(result.owners);

            $("#dialog:ui-dialog").dialog("destroy");

            $("#dialog-confirm").dialog({
            resizable: false,
            height: 600,
            modal: true,
            open: function() {
                $(this).children('div.dialog-text').replaceWith("<h3><b>Users</b></h3>" + makeDialogTable(users) + "<h3><b>Owners</b></h3>" + makeDialogTable(owners));
            },

            buttons: {
                Okay: function() {
                $(this).dialog("close");
// problem
                    $.ajax({
                        type: "GET",
                        url: "/cgi-bin/ajax.pl",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",

                        // generate and send parameters to server-side script
                    data: $(this).serialize(),

                    // script call was *not* successful
                    error: function(XMLHttpRequest, textStatus, errorThrown) { 
                        $('div#create_result').text("responseText: " + XMLHttpRequest.responseText + ", textStatus: " + textStatus + ", errorThrown: " + errorThrown);
                        $('div#create_result').addClass("error");
                    },

                    success: function(result2){
                        if (result2.error) { // script returned error
                        $('div#create_result').text("result2.error: " + result2.error);
                        $('div#create_result').addClass("error");
                        } else { // perl script says everything is okay
                        $('div#create_result').text("result2.success: " + result.success + ", result2.id: " + result.id);
                        $('div#create_result').addClass("success");
                        } //else
                    } // success
                    }); // ajax

                } else { // if (is_okay) { ...
                    $('div#create_result').text("Fill out the form to create an activity");
                    $('div#create_result').addClass("error");
                } // else
                }, // Okay

// problem
                Cancel: function() {
                is_okay = 0;
                $(this).dialog("close");
                }
            } // buttons
            }); // dialog
        } //else
        } // success
    }); // ajax

    // ...
$(文档).ready(函数(){
$('form').live('submit',function(){
// ...
$.ajax({
键入:“获取”,
url:“/cgi-bin/ajax\u-confirm.pl”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
//async:false,
数据:$(this).serialize(),
错误:函数(XMLHttpRequest、textStatus、ErrorSprown){
$('div#create_result').text(“responseText:+XMLHttpRequest.responseText+
,textStatus:“+textStatus”+
,errorhorn:“+errorhorn”;
$('div#create_result').addClass(“错误”);
警报(“ajax.js确认代码中出现错误”);
},
成功:功能(结果){
if(result.error){
$('div#create_result').text(“result.error:+result.error”);
$('div#create_result').addClass(“错误”);
}否则{//服务器端脚本会说一切正常
var users=$.parseJSON(result.users);
var owners=$.parseJSON(result.owners);
$(“#dialog:ui dialog”).dialog(“销毁”);
$(“#对话框确认”)。对话框({
可调整大小:false,
身高:600,
莫代尔:是的,
打开:函数(){
$(this).children('div.dialog-text')。替换为(“用户”+makeDialogTable(用户)+“所有者”+makeDialogTable(所有者));
},
按钮:{
好的:函数(){
$(此).dialog(“关闭”);
//问题
$.ajax({
键入:“获取”,
url:“/cgi-bin/ajax.pl”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
//生成参数并将其发送到服务器端脚本
数据:$(this).serialize(),
//脚本调用*未*成功
错误:函数(XMLHttpRequest、textStatus、ErrorSprown){
$('div#create_result').text(“responseText:+XMLHttpRequest.responseText+”,textStatus:+textStatus+”,errorboorn:+errorboorn);
$('div#create_result').addClass(“错误”);
},
成功:功能(结果2){
if(result2.error){//脚本返回错误
$('div#create_result').text(“result2.error:+result2.error”);
$('div#create_result').addClass(“错误”);
}否则{//perl脚本表示一切正常
$('div#create_result').text(“result2.success:+result.success+”,result2.id:+result.id);
$('div#create_result').addClass(“success”);
}//否则
}//成功
});//ajax
}否则{//如果(你还好的话){。。。
$('div#create_result')。文本(“填写表格以创建活动”);
$('div#create_result').addClass(“错误”);
}//否则
},//好的
//问题
取消:函数(){
_是否正常=0;
$(此).dialog(“关闭”);
}
}//按钮
});//对话框
}//否则
}//成功
});//ajax
// ...
更新 下面是HTML,显示时如下所示


标题:
所有者:

课程
创造 对话框文本在这里

身份证件 标题 所有者 开始日期 结束日期 类型 个性化
课程 使用者 组
在问题区域的这一行

data: $(this).serialize(),
“this”指的是对话框本身,也可能是ajax

您需要引用您试图序列化的特定表单

我会将该值设置为一个变量,并记录它以测试/查看那里发生了什么

所以首先要弄清楚是什么

 $(this) 
是通过跑步

alert($(this).prop('id')) // or something similar if your using older jQuery use .attr instead of .prop
编辑 看起来你没有给表单一个id,所以我不是100%确定,但我认为你可以这样做

$('form').live('submit', function(){
var aform = $(this)
.....
然后在problam地区

 data: $(aform).serialize(),

尽量减少嵌套:(.将其抽象为对象和函数。不内联所有匿名函数会使事情变得更简单。我们可以看到与此相关的html吗?@mcgrailm:当然。还添加了一个屏幕截图。它是“创建”我点击按钮。如何在Ajax调用中执行此操作?我的意思是,如果我执行
警报(printObject($(this).serialize());
,则在Ajax调用的成功区域中执行此操作,因为“this”不指向要序列化的表单,因此需要使用指向
 data: $(aform).serialize(),