2011-11-28 46 views
5

我失去了我的希望找到解決我的問題,希望有人可以幫助我在這裏。這裏的問題:Wordpress 301移動永久響應Jquery.post請求

我有一個名爲wp_lojas(用於商店)的額外表wordpress安裝。客戶可以訪問該網站並通過表單,放置地址,然後使用Google Maps API查找附近的商店。

當用戶提交表單時,我會驗證他們的地址位置(緯度/經度),並通過SQL查詢查找最近的商店。

表單通過ajax請求($ .post())將參數(緯度/經度/半徑)發送到我的主題文件夾內的php文件(/wp-content/themes/accessorize/dadosLojas.php )並且該文件使用找到的商店構建XML。

一切工作正常離線,在我的本地機器上。在線我得到一個答案「301永久移動」。如果你們安裝了Firebug並想嘗試,測試鏈接是http://www.colletivo.com.br/accessorize/,表單位於頁面的頁腳上。如果你們想要來自巴西的地址,請嘗試「Rua Vicente Leporace,1534」。

如果您不明白我試圖解釋什麼,或需要更多信息,請告訴我。

非常感謝。

回答

1

問題解決了Greg Pettit給出的提示!

我不知道Wordpress攔截Ajax請求的可能性,我認爲它與htaccess將請求重定向到不存在的URL有關。

知道了這一點,我研究瞭如何使用Wordpress的功能,使Ajax請求,這裏是解決方案:

在我的主題的functions.php中:

// Hooks wp_ajax that points to the function that builds the XML Stores 
add_action('wp_ajax_procura_lojas','procuraLojas'); // Unlogged User 
add_action('wp_ajax_nopriv_procura_lojas','procuraLojas'); // Logged User 

// Function that Builds Stores XML 
function procuraLojas() { 

    global $wpdb; 

    // Retrieve $_POST informattion coming from jQuery.post 
    $lat = $_POST["latitude"]; 
    $lon = $_POST["longitude"]; 
    $raio = $_POST["raio"]; 

    // Query wp_lojas and Build XML 
    $consulta = $wpdb->get_results(sprintf("SELECT * , (3959 * acos(cos(radians('%s')) * cos(radians(latitude)) * cos(radians(longitude) - radians('%s')) + sin(radians('%s')) * sin(radians(latitude)))) AS distancia FROM wp_lojas HAVING distancia < '%s' ORDER BY distancia", 
    mysql_real_escape_string($lat), 
    mysql_real_escape_string($lon), 
    mysql_real_escape_string($lat), 
    mysql_real_escape_string($raio))); 

    $dom = new DOMDocument("1.0", "utf-8"); 
    $no = $dom->createElement("lojas"); 
    $parnode = $dom->appendChild($no); 

    header("Content-type: text/xml"); 

    foreach ($consulta as $loja){ 

     $no = $dom->createElement("loja"); 
     $novono = $parnode->appendChild($no); 

     $novono->setAttribute('nome',   $loja->nome); 
     $novono->setAttribute('lat',   $loja->latitude); 
     $novono->setAttribute('lon',   $loja->longitude); 
     $novono->setAttribute('telefone',  $loja->telefone); 
     $novono->setAttribute('email',   $loja->email); 
     $novono->setAttribute('endereco',  $loja->endereco); 
     $novono->setAttribute('numero',   $loja->numero); 
     $novono->setAttribute('complemento',  $loja->complemento); 
     $novono->setAttribute('bairro',   $loja->bairro); 
     $novono->setAttribute('cidade',   $loja->cidade); 
     $novono->setAttribute('estado',   $loja->estado); 
     $novono->setAttribute('cep',   $loja->cep); 
     $novono->setAttribute('distancia',  $loja->distancia); 

    } 

    // Print XML and Exit 
    echo $dom->saveXML(); 
    exit; 

} 

在WordPress的頭,我把:

<script type='text/javascript'> 
/* <![CDATA[ */ 
var MyAjax = { ajaxurl: "<?php bloginfo('url'); ?>/wp-admin/admin-ajax.php" }; // Build the Link to admin-ajax.php/Javascript Global Variable 
/* ]]> */ 
</script> 

當用戶提交表單,我發送POST AJAX的類的WordPress是負責這可溼性粉劑管理員/管理員-ajax.php「:

jQuery.post(
    MyAjax.ajaxurl, // Link to the file 'wp-admin/admin-ajax.php' responsible for handling ajax requisitions 
    { 
     action : 'procura_lojas', // Name used in the hook 'wp_ajax_procura_lojas' 
     latitude : center.lat(), // Latitude Parameter 
     longitude : center.lng(), // Longitude Parameter 
     raio : raio // Radius Parameter 
    }, 
    function(data) { // Callback 

     // Retrieve all nodes called 'loja' and put it in the map 
     var markers = data.documentElement.getElementsByTagName("loja"); 
     for (var i = 0; i < markers.length; i++) { 
      var dados = []; 
      dados["nome"] = markers[i].getAttribute('nome'); 
      dados["estado"] = markers[i].getAttribute('estado'); 
      dados["cidade"] = markers[i].getAttribute('cidade'); 
      dados["bairro"] = markers[i].getAttribute('bairro'); 
      dados["endereco"] = markers[i].getAttribute('endereco'); 
      dados["numero"] = markers[i].getAttribute('numero'); 
      dados["complemento"] = markers[i].getAttribute('complemento'); 
      dados["cep"] = markers[i].getAttribute('cep'); 
      dados["telefone"] = markers[i].getAttribute('telefone'); 

      var latlng = new google.maps.LatLng(parseFloat(markers[i].getAttribute('lat')), parseFloat(markers[i].getAttribute('lon'))); 
      var marker = createMarker(markers[i].getAttribute("name"), latlng, dados); 
     } 
    } 
); 

就是這樣!感謝Greg Pettit澄清我的想法。

一個鏈接,助我也爲5 tips for using ajax in wordpress

我希望這個答案可以幫助別人一天。

0

Wordpress將選擇您的AJAX請求給任何給定的PHP文件。這對我來說都是黑魔法,所以有更好答案的人很快就會出現。儘管如此,你需要通過一個特殊的腳本管理你的AJAX請求,這個腳本被設計用來適當地路由請求。默認情況下,這是用ajax-admin.php完成的(或者是admin-ajax.php?)

我希望我有更多的非正式的細節,但我想至少讓你知道,很多圈子,您需要在Wordpress中處理AJAX對自定義腳本的請求。

+0

我明白了!好吧,我想我可以繼續尋找那條路。謝謝 –

0

我今天有同樣的奇怪問題,但隻影響用戶角色中的用戶。萬惡的根源是一個小功能,它應該防止用戶訪問後端。

function preventAccessToBackend() {  
    if(!current_user_can('edit_posts')) { 
     wp_redirect(home_url()); exit; 
    } 
} 
add_action('admin_init', 'preventAccessToBackend'); 

正如你所看到的,在功能連接到admin_init掛鉤,因此對管理員執行,也一個AJAX調用。

該解決方案很簡單。在重定向之前檢查它是否是AJAX請求。

function preventAccessToBackend() {  
    if (defined('DOING_AJAX') && DOING_AJAX) { return; } 

    if(!current_user_can('edit_posts')) { 
     wp_redirect(home_url()); exit; 
    } 
} 
add_action('admin_init', 'preventAccessToBackend');