$ _POST取消设置并使用parse_str数据重置

时间:2016-12-06 05:21:31

标签: javascript php jquery ajax wordpress

我正在尝试执行页面的快速AJAX预览,该页面使用$_POST(序列化表单数据)重新定义超级全局parse_str变量。基本上,我想快速做到这一点,并成功地在localhost上执行此操作,但似乎在实际的实时服务器上失败了。不确定这是否是由wordpress多站点引起的......

但基本上,我在AJAX中序列化表单(这是通过$ _POST发送的相同表单)。我在php函数中做parse_str($formData, $_POST);,其中$ data是来自AJAX请求的序列化表单数据。

我已将global $_POST;放在AJAX php函数的顶部以覆盖它。这是我的php ajax函数代码,可以在localhost上正常工作:

function hunter_preview_quote()
{
    global $_POST;

    check_ajax_referer('preview-quote', 'security');

    $response = array(
        'error' => 'Error Occurred while attempting to generate a preview for this quote. Please try again.'
    );

    if (!current_user_can('manage_options'))
    {
        $response['error'] = 'You do not have permission to view this.';
        echo json_encode($response);
        die();
    }

    $formData = $_POST['form'];
    unset($_POST);

    // Rewrite the Global $_POST data send with the form, with that from the serialized form array!
    parse_str($formData, $_POST);

    ob_start();
    hunter_admin_build_form_html(false);
    $content = ob_get_contents();
    ob_end_clean();

    if (!empty($content))
        $response['content'] = $content;

    echo json_encode($response);
    die();
}

它正在调用hunter_admin_build_form_html,它负责从表单中获取$ _POST数据并构建预览。 param false告诉它不要发送电子邮件。

无论如何,这不适用于实际网站,我不确定它是否与该网站上安装的Wordpress Multisite有关。这里返回的响应应该发生的事情如下:

function OpenPopupWindow(content, title, w, h, opts) {
   var _innerOpts = '';
   if(opts !== null && typeof opts === 'object' ){
       for (var p in opts ) {
           if (opts.hasOwnProperty(p)) {
               _innerOpts += p + '=' + opts[p] + ',';
           }
       }
   }
     // Fixes dual-screen position, Most browsers, Firefox
   var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
   var dualScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;

   var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
   var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;

   var left = ((width / 2) - (w / 2)) + dualScreenLeft;
   var top = ((height / 2) - (h / 2)) + dualScreenTop;
   var w = window.open('', title, _innerOpts + ' width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);

   $(w.document.body).html('<!doctype html><html><head><meta charset="utf-8"><title>' + title + '</title></head>' + content + '</html>');

    // Puts focus on the newWindow
   if (window.focus) {
       w.focus();
   }

   return w;
}

$("#submit-preview-quote").click(function(event) {

    event.preventDefault();

    var $this = $(this),
        $formData = $this.closest('form').serialize();

    var data = {
        action: 'hunter_preview_quote',
        security: HUNTER_CONTACTFORM_admin['preview_quote_nonce'],
        form: $formData
    };

    $.ajax({
        type: 'POST',
        url: HUNTER_CONTACTFORM_admin.ajax_url,
        data: data,
        cache: false,
        dataType: 'json'
    }).done(function(response) {

        // Load the Preview popup now... if all is fine!
        if (response.hasOwnProperty('content'))
            var w = OpenPopupWindow(response['content'], 'Quote Preview', '700', '500', {toolbar: 'no', location: 'no', status: 'no', menubar: 'no', scrollbars: 'yes', resizable: 'yes'} );
        else if (response.hasOwnProperty('error'))
            alert(response['error']);

    }).fail(function(response) {

        if (response.hasOwnProperty('error'))
            alert(response['error']);

    }).always(function(response) {
        // Nothing needed in here...
    });

});

因此,它应该在新窗口中打开预览,但相反,我在警告框中获取疯狂的javascript值,如下所示:

function (){return f&&(c&&!b&&(h=f.length-1,g.push(c)),function d(b){n.each(b,function(b,c){n.isFunction(c)?a.unique&&j.has(c)||f.push(c):c&&c.length&&"string"!==n.type(c)&&d(c)})}(arguments),c&&!b&&i()),this}

不确定导致这种情况发生的原因,但似乎某处出现了错误,但为什么要推出js功能呢?更深入的研究证实,AJAX实际上是以某种方式失败了。所以它正在击中ajax的.fail部分。

原来这是由于NewRelic推送的js没有经过正确的json编码,所以需要禁用它!

1 个答案:

答案 0 :(得分:0)

似乎这是New Relic的一个问题。在ajax函数中,刚刚执行了此操作并删除了newrelic的脚本内容:

if (extension_loaded ('newrelic')) {
    newrelic_disable_autorum();
}

感谢您对Rasclatt的帮助!