Ajax调用在回显后不返回数据

时间:2013-12-13 14:41:05

标签: php ajax jquery

ajax调用成功更新记录但不返回echo字符串。此脚本之前正在运行,但可能是因为某些升级它停止工作。它在我的本地系统上工作正常但是当我将它移动到bluehost服务器时它就不起作用了。

这是我的Ajax调用:

// call to ajax script to update site
        function autopost_update_site() {

            // get base directory url
            var baseDir = jQuery('#baseDir').val();
            var id = jQuery('#site_id').val();
            var site_name = jQuery('#site_name').val();
            var site_url = jQuery('#site_url').val();
            var is_active;
            if ( jQuery('#active_radio').is(':checked') ){
                is_active = '1';
            } else {
                is_active = '0';
            }

            var username = jQuery('#login_username').val();
            var login_pass = jQuery('#login_pass').val();

            // call to ajax script to update script
            jQuery.ajax( {
                type: "POST",
                url: baseDir + "/autopost_ajax_actions.php",
                data: "id=" + id + "&site_name=" + site_name + "&site_url=" + site_url + "&is_active="+ is_active + "&username=" + username + "&login_pass="+login_pass +"&action=update_site",


                beforeSend: function() {
                    jQuery('#update_site_button').attr("disabled", true);
                    // shortcode edit button
                    jQuery('#update_site_button').html('Updating...');
                    // admin page edit button
                    jQuery('#update_site_button').val('Updating...');
                },
                complete: function() {
                    jQuery('#update_site_button').attr("disabled", false);
                    // shortcode edit button
                    jQuery('#update_site_button').html('Update');
                    // admin page edit button
                    jQuery('#update_site_button').val('Update');
                },
                success: function(data) {
                    alert("Result: " + data); // NOTHING IS HAPPENING HERE, NO ALERT DATA
                    if (jQuery.trim(data) === "success") {
                        alert("Site updated.");
                        // refresh page
                        window.setTimeout('location.reload()', 200);
                    } else {
                        alert("Some error occured, Please try again.");
                    }
                }
            });
        }

这是我用于ajax操作的自定义php脚本:

// update site
    if ( $_POST['action'] == 'update_site' && isset ($_POST['id']) ) {

            // collect site data
            $site_name = $wpdb->escape($_POST['site_name']);
            $site_id = intval($wpdb->escape($_POST['id']));
            $site_url = $wpdb->escape($_POST['site_url']);
            $username = $wpdb->escape($_POST['username']);
            $login_pass = $wpdb->escape($_POST['login_pass']);
            $is_active = $wpdb->escape($_POST['is_active']);

            $query = $wpdb->prepare("UPDATE " . $autopost_sites_table_name . " SET site_name = %s, site_url = %s, username = %s, login_pass = %s, is_active = %s WHERE id = %s", $site_name, $site_url, $username, $login_pass, $is_active, $site_id);               
            #$log->LogDebug($query);
            // execute query
            $result = $wpdb->query($query);

            #$log->LogDebug($result);

            if ( $result !== FALSE ||  $result !== 0) {

                // return success
                $response = "success";
                #$log->LogDebug($response);
                echo $response; // THIS RESPONSE IS NOT SHOWING ON AJAX SUCCESS 

            } else {
                $log->LogError("Failed to update site with ID: " . $_POST['id']);
                echo "Failed to update site.";
            }

    }

谁能告诉我缺少什么?

3 个答案:

答案 0 :(得分:1)

按如下方式更改 data

data:{
      id:id,
      site_name:site_name,
      site_url:site_url ,
      is_active:is_active ,
      username:username,
      login_pass:login_pass,
      action:"update_site"
    }

答案 1 :(得分:1)

正如提到的那样,脚本在本地工作得很好但在服务器上没有工作。所以这是一些服务器问题,我在chrome网络中遇到“Access-Control-Allow-Origin”错误。

我刚刚添加了

header('Access-Control-Allow-Origin: *'); 

到我的ajax动作脚本,它工作了!感谢

答案 2 :(得分:-2)

echo $response;

die();

请参阅http://codex.wordpress.org/AJAX_in_Plugins

在示例中它说:

function my_action_callback() {
    global $wpdb; // this is how you get access to the database

    $whatever = intval( $_POST['whatever'] );

    $whatever += 10;

    echo $whatever;

    die(); // this is required to return a proper result
}