简体   繁体   English

从jQuery AJAX调用返回信息,并在成功执行其他jQuery

[英]Return info from jQuery AJAX call and execute additional jQuery on success

I have the following jQuery AJAX to duplicate a background image. 我有以下jQuery AJAX复制背景图像。 I am stumped as to how to effectively return information back to the original page. 我对如何有效地将信息返回到原始页面感到困惑。 Here is the AJAX I send on click of "'#dupBtn"... 这是我通过点击“'#dupBtn”发送的AJAX ...

//DUPLICATE BACKGROUND      
$('#dupBtn').click(function() {
    jQuery.ajax({
            type: "POST",
            dataType:'json',
            url: "../system/bgUpdate.php",
            data: {
                "user":<?= $_POST['user'] ?>,
                "bgID":bgID,
                "refID2":<?= $_POST['refID2'] ?>,
                "refTable":"<?= $_POST['refTable'] ?>",
                "bgTitle":($('#bgTitle').val()),
                "path":path,
                "bgColor":bgColor,
                "bgPoz":bgPoz,
                "bgRepeat":bgRepeat,
                "attach":attach
                }
        });
});

Here is the basic MySQL query on the PHP page bgUpdate.php. 这是PHP页面bgUpdate.php上的基本MySQL查询。

mysql_query("INSERT INTO backgrounds (user,title,path,bgColor,bgPosition,bgRepeat,bgAttachment) VALUES ('".$_POST['user']."','$title','".$_POST['path']."','$bgColor','".$_POST['bgPoz']."','$rt','$attach')");

$bgIDnew = mysql_insert_id();

What I want to do is have the following code fired on the original page upon successful execution of the MySQL entry, dynamically catching the '$bgIDnew' from the MySQL PHP page. 我要做的是在成功执行MySQL条目后在原始页面上触发以下代码,以动态捕获MySQL PHP页面中的“ $ bgIDnew”。

$('#bgPickerBox').load('../dialog/bgPickerAlbums.php?album=<?=$bgIDnew?>');

jQuery.ajax() has a success property that acts as a callback that you can use. jQuery.ajax()具有success属性,可以用作您可以使用的回调。 Another is complete which is fired if the request is successful or not. 另一个complete的请求是否成功将被触发。

jQuery.ajax({
  /* your stuff here */
  success: function(response) {
    $('#bgPickerBox').load('../dialog/bgPickerAlbums.php?album=<?=$bgIDnew?>');
  }
});

You can write up the logic in the success callback function of your ajax Request.. 您可以在ajax请求的成功回调函数中编写逻辑。

This is fired when an ajax request is successfully returned.. 成功返回ajax请求时将触发此事件。

success: function(response) {
    $('#bgPickerBox').load('../dialog/bgPickerAlbums.php?album=<?=$bgIDnew?>');
  }

Add this to your ajax Request... 将此添加到您的ajax请求中...

You can accomplish this with the success attribute of the .ajax() function: 您可以使用.ajax()函数的success属性来完成此操作:

$('#dupBtn').click(function() {
    jQuery.ajax({
        type: "POST",
        dataType:'json',
        url: "../system/bgUpdate.php",
        data: {
            ...
            },
        success:
            function(response)
            {
                $('#bgPickerBox').load('../dialog/bgPickerAlbums.php?album=<?=$bgIDnew?>');
            }
    });
});

That's only part of it though... The other half is that your PHP needs to return something that jQuery can understand as a "successful" call. 不过,这只是其中的一部分...另一半是您的PHP需要返回一些jQuery可以理解为“成功”的调用。 My preference is to use HTTP status codes. 我的偏好是使用HTTP状态代码。 In your case, your PHP script should return a 200 code if it was successful; 对于您而言,如果成功,PHP脚本应返回200代码; otherwise, it should return something in the 400 range. 否则,它将返回400范围内的值。 (By the way, if you want jQuery to do something separate with errors, you can use the error property of .ajax() .) (顺便说一句,如果您希望jQuery对错误进行单独处理,则可以使用.ajax()error属性。)

However, if you need to return data from the server to the client-side script, then your PHP can print out that information like this: 但是,如果您需要将数据从服务器返回到客户端脚本,那么您的PHP可以打印出如下信息:

mysql_query("INSERT INTO backgrounds (user,title,path,bgColor,bgPosition,bgRepeat,bgAttachment) VALUES ('".$_POST['user']."','$title','".$_POST['path']."','$bgColor','".$_POST['bgPoz']."','$rt','$attach')");

$bgIDnew = mysql_insert_id();
// Best practice to keep it in some sort of understandable format
// Here, we'll put it in an associative array:
$response = array('id' => $bgIDnew);
print_r(json_encode($response));

This PHP script sends back to the ajax() method a JSON representation of the $response variable. 该PHP脚本将$ response变量的JSON表示发送回ajax()方法。 You've already configured that ajax() method to read the response dataType as JSON, so it already knows how to read the response parameter... Which means your success function can look something like this: 您已经配置了ajax()方法来将响应数据类型读取为JSON,因此它已经知道如何读取response参数...这意味着您的成功函数可以如下所示:

success:
    function(response)
    {
        $('#bgPickerBox').load('../dialog/bgPickerAlbums.php?album=' + response.id);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM