doulan2827 2019-05-15 12:57
浏览 57

使用HTTP请求解析admin-ajax中的脚本标头时出错

I am working on Wordpress and I have a strange error. This is my jQuery code

add_to_my_cart: function () {
            $(document).on('submit', 'form.form-submit', function (event) {
                event.preventDefault();              
                var data = $(this).serializeArray();

                $.ajax({
                    url     : wp_custom_settings.ajax,
                    type    : 'POST',
                    data    : data,
                    dataType: 'html',
                    success : function (code) {
                        code = parseJSON(code);
                        window.location.href = code.redirect;
                    }
                });
            });
        }

The above is called when I submit a form and pass some data to the PHP function. However, I get a 500 internal server error on admin-ajax HTTP request, with the following on apache logs.

Premature end of script headers: admin-ajax.php, referer: my.domain.here
AH01070: Error parsing script headers, referer: my.domain.here

I used the error_log($message) in every step of the process and I haven't found any problem. The very last step is the return of the JSON request.

if ( ! function_exists( 'my_send_json' ) ) {

function my_send_json( $data ) {
    error_log("DONE");
    @header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
    echo '<!-- WP_AJAX_START -->';
    echo wp_json_encode( $data );
    echo '<!-- WP_AJAX_END -->';
    if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
        wp_die();
    } else {
        die;
    }
}
}

I can see on the debug.log the string DONE, but the HTTP request has an empty response. I tried also to remove the $data and just return this

echo wp_json_encode( array( 'status' => 'success', 'redirect' => 'my.domain.here' ));

But still the same. I cannot think of any other way to debug it.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测