2016-12-06 53 views
0

我试图进行重新定义与从parse_str(连载的形式数据)的超级全球$_POST变量页面的快速预览AJAX。基本上,我想快速做到这一点,并成功地在localhost上做到这一点,但似乎在实际的现场服务器上失败。不知道这是否是由于wordpress多站点或不...

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

我已经把global $_POST;放在AJAX php函数的顶部来覆盖它。这里是我的PHP AJAX功能的代码,在本地主机上正常工作:

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的失败部分。

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

+0

我不知道我是否会像这样''_POST'乱。你只是想通过全球/超范围的数据是全部? – Rasclatt

+0

另外,您可以在控制台中跟踪该警报吗?也许如果你知道它来自哪里,你可以更容易地找到做什么。但这是一个奇怪的匿名函数。或者,如果您有权访问所有文件,只需搜索该字符串,它就会告诉您来自哪里。 – Rasclatt

+0

我试图使用ajax使用相同的$ _POST变量从窗体生成预览,使用同样的函数也会生成一个电子邮件,以便我可以在发送之前向客户端显示电子邮件预览。 –

回答

0

看起来这是New Relic的问题。在AJAX功能,只是做这个和它删除脚本工具和NewRelic的:

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

感谢这个Rasclatt您的帮助!