2016-12-06 59 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您的幫助!